2015-10-11 55 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

親愛的朋友, 感謝您的幫助! –

相關問題