这是我用 onTouchListener 编写的 Confirm Dialogue 代码:
new AlertDialog.Builder(getApplicationContext())
.setTitle("Submit Quiz")
.setMessage("Are you sure to submit the quiz")
.setIcon(R.drawable.icon)
.setPositiveButton("Yes",
new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
submitQuizConfirmed();
}
})
.setNegativeButton("No",
new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
}
).show();
这会引发空指针异常。我真的被这个小错误困住了。请帮我。
更新:Logcat 异常堆栈跟踪:
02-14 13:06:07.023: WARN/WindowManager(59): Attempted to add window with non-application token WindowToken{44f0fbc8 token=null}. Aborting.
02-14 13:06:07.023: DEBUG/AndroidRuntime(2036): Shutting down VM
02-14 13:06:07.023: WARN/dalvikvm(2036): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): FATAL EXCEPTION: main
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewRoot.setView(ViewRoot.java:509)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
0 2-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.app.Dialog.show(Dialog.java:241)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at uk.co.planetbeyond.service.Quiz$QuizQuestionsAdapter.submitQuizPressed(Quiz.java:146)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at uk.co.planetbeyond.service.Quiz$QuizQuestionsAdapter.moveToNextPage(Quiz.java:122)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at uk.co.planetbeyond.service.Quiz$MyGestureDetector.onFling(Quiz.java:86)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:568)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at uk.co.planetbeyond.service.Quiz$1.onTouch(Quiz.java:57)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.View.dispatchTouchEvent(View.java:3762)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.os.Looper.loop(Looper.java:123)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): at dalvik.system.NativeStart.main(Native Method)