为什么我出现NullPointerException? [英] Why i got NullPointerException ?
本文介绍了为什么我出现NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码已建立好:
Code builded ok:
Button buttonCancel = (Button)findViewById(R.id.Cancel);
try{
buttonCancel.setOnClickListener(buttonCancelOnClickListener);
}catch(Exception x)
{
x.toString();//NullPointerException
}
在text_enter.xml中:
In text_enter.xml:
<Button
android:id="@+id/Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/SendText"
android:layout_alignBottom="@+id/SendText"
android:layout_alignParentLeft="true"
android:text="Cancel" />
buttonCancel = null,为什么?请帮助.
buttonCancel = null, why? Please, help.
推荐答案
尽管您没有提供足够的信息,但可以尝试这样做.
Though you not provide enough information but you can try like this..
private Button buttonCancle;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.wishingpage);
buttonCancel = (Button)findViewById(R.id.Cancel);
buttonCancel.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.buttonCancel:
//do your button work here..you can add more button event using case
break;
}
}
已解决.我必须
setContentView(R.layout.text_enter);
在声明并设置它的侦听器之前,其中包含我的取消"按钮.
that contains my Cancel button, before declaring and setting listener for it.
这篇关于为什么我出现NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文