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。我做错了什么?
喵喔喔
蓝山帝景
慕森卡
开满天机
随时随地看视频慕课网APP
相关分类