我已经设置了一个按钮,当用户点击它时我试图显示一个烤面包。这里是我的Java代码 -Onclick监听器抛出NullPointerException
file = (Button) findViewById(R.id.file);
file.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Display the file chooser dialog
//showChooser();
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();
}
});
这里是我的XML代码设置按钮 -
<Button
android:id="@+id/file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/plt"
android:text="File" />
这将引发一个NullPointerException就行了file.setOnClickListener(new OnClickListener() {
。我究竟做错了什么?
我会补充一点,“干净的项目”是一个潜在的修复的原因是因为有时(使用Eclipse),'R.java'与您编写的XML布局不同步,干净的构建将重新生成从头开始编写'R.java' – ataulm
当然,在使用eclipse时这是一个非常常见的问题。 – scriptocalypse