我在这行收到错误:
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)
您正在尝试将小部件(Button
)强制转换为自定义类(也称为Button
)。将自定义类重命名为更能说明其功能的内容,或者使用按钮小部件的完整包标识符,如下所示。
final android.widget.Button addBtn = (android.widget.Button) findViewById(R.id.btnAdd);