2015-10-11 31 views
-4

我在这条线在这里得到一个错误:无法施展 'android.view.View' 到 'com.example.avivyaacobi.contactmanager.Button'

final Button addBtn = (Button) findViewById(R.id.btnAdd); 

我的代码:

package com.example.avivyaacobi.contactmanager; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.EditText; 
import android.widget.TabHost; 

public class MainActivity extends Activity { 

    EditText nameTxt, phoneTxt, emailTxt, addressTxt; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     nameTxt = (EditText) findViewById(R.id.txtName); 
     phoneTxt = (EditText) findViewById(R.id.txtPhone); 
     emailTxt = (EditText) findViewById(R.id.txtEmail); 
     addressTxt = (EditText) findViewById(R.id.txtAddress); 
     TabHost tabHost = (TabHost) findViewById(R.id.tabHost); 

     tabHost.setup(); 

     TabHost.TabSpec tabSpec= tabHost.newTabSpec("creator"); 
     tabSpec.setContent(R.id.tabCreator); 
     tabSpec.setIndicator("Creator"); 
     tabHost.addTab(tabSpec); 

     tabSpec= tabHost.newTabSpec("list"); 
     tabSpec.setContent(R.id.tabContactList); 
     tabSpec.setIndicator("List"); 
     tabHost.addTab(tabSpec); 

     final Button addBtn = (Button) findViewById(R.id.btnAdd); 

     nameTxt.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
      addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty()); 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

我有功能按钮:

package com.example.avivyaacobi.contactmanager; 

/** 
* Created by AvivYaacobi on 10/1/15. 
*/ 
public class Button { 
    private boolean enable; 

    public void setEnabled(boolean enabled) { 
     this.enable = enabled; 
    } 
} 

我不得不说,我是新使用此工具(Android Studio中)和Java,我不知道是什么问题。

+0

如果你的按钮自定义视图组件然后应该把它扩展视图,且具有相应的实施...... –

回答

4

您正试图将Widget(Button)投射到您的自定义班级(也称为Button)。要么将您的自定义类重命名为更具指示性的类,要么使用按钮构件的完整包标识符,如下所示。

final android.widget.Button addBtn = (android.widget.Button) findViewById(R.id.btnAdd); 
+0

亲爱的朋友, 感谢您的帮助! –

相关问题