出现一个显示图像的对话框。我在对话框内有一个按钮应该关闭对话框,所以它是一个退出按钮。当按下它时,我得到一个空指针异常。
这是我包含对话框的代码:
buildMoreButton = (Button) findViewById(R.id.buildMore);
buildMoreButton.setText("BUILD");
buildMoreButton.setBackgroundColor(-65536);
buildMoreButton.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
buildMoreButton.setBackgroundColor(-1);
}//end if
else if (event.getAction() == MotionEvent.ACTION_UP)
{
buildMoreButton.setBackgroundColor(-65536);
}//end else if
//open dialog for building choices
buildingSelect = new Dialog(runGraphics.this);
buildingSelect.setContentView(R.layout.dialog);
buildingSelect.setTitle("Building Selection");
exit = (Button) findViewById(R.id.exit);
exit.setText("X");
exit.setBackgroundColor(-65536);
exit.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
exit.setBackgroundColor(-1);
}//end if
else if (event.getAction() == MotionEvent.ACTION_UP)
{
exit.setBackgroundColor(-65536);
}//end else if
buildingSelect.dismiss();
return false;
}//end onTouch
});//end OnTouchListener
colonyHut = (ImageView) findViewById(R.id.colonyHut);
buildingSelect.show();
return false;
}//end onTouch function
});//end OnTouchListener
这是 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/colonyHut"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/mainhut" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right" />
</LinearLayout>
这是我的 logcat:
06-11 18:50:29.228: E/AndroidRuntime(28480): FATAL EXCEPTION: main
06-11 18:50:29.228: E/AndroidRuntime(28480): java.lang.NullPointerException
06-11 18:50:29.228: E/AndroidRuntime(28480): at com.project.llb.runGraphics$6.onTouch(runGraphics.java:329)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.View.dispatchTouchEvent(View.java:3881)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1923)
06-11 18:50:29.228: E/AndroidRuntime(28480): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1190)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.app.Activity.dispatchTouchEvent(Activity.java:2155)
06-11 18:50:29.228: E/AndroidRuntime(28480): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1907)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2197)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.view.ViewRoot.handleMessage(ViewRoot.java:1881)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.os.Looper.loop(Looper.java:130)
06-11 18:50:29.228: E/AndroidRuntime(28480): at android.app.ActivityThread.main(ActivityThread.java:3806)
06-11 18:50:29.228: E/AndroidRuntime(28480): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:50:29.228: E/AndroidRuntime(28480): at java.lang.reflect.Method.invoke(Method.java:507)
06-11 18:50:29.228: E/AndroidRuntime(28480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 18:50:29.228: E/AndroidRuntime(28480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 18:50:29.228: E/AndroidRuntime(28480): at dalvik.system.NativeStart.main(Native Method)
提前感谢大家的帮助。
最佳答案
您获得了错误的 View 引用,因此给您NPE,您正在使用findViewById
,它将找到不在对话框布局中的 Activity 布局的 View ,而是调用Dialog
的findViewById
来获取 View
解决方案:
exit = (Button) buildingSelect.findViewById(R.id.exit);
colonyHut = (ImageView) buildingSelect.findViewById(R.id.colonyHut);
关于java - 用于关闭对话框的按钮引发 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24174484/