findViewById返回null我有代码:public class HelloWorld extends Activity {private Button buttonOk;private Button buttonCancel;private OnClickListener buttonOkListener = new OnClickListener() {
public void onClick(View v){
EditText editText = (EditText)findViewById(R.id.input);
CharSequence textFromInput = (CharSequence) editText.getText();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,textFromInput,duration);
toast.show();
}};private OnClickListener buttonCancelListener = new OnClickListener() {
public void onClick(View v){
EditText editText = (EditText)findViewById(R.id.input);
CharSequence textFromInput = (CharSequence) editText.getText();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,textFromInput,duration);
toast.show();
}};/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(R.layout.main);
buttonOk = (Button)findViewById(R.id.buttonOk);
buttonCancel = (Button)findViewById(R.id.buttonCancel);
buttonOk.setOnClickListener(buttonOkListener);
buttonCancel.setOnClickListener(buttonCancelListener);}}使用buttonCancel.setOnClickListener(buttonCancelListener); 抛出异常因为buttonCancel为null。我做错了什么?
- 3 回答
- 0 关注
- 490 浏览
添加回答
举报
0/150
提交
取消