这是我的确认对话代码,用onTouchListener编写:
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();
这会抛出nullpointerexception。我真的被这个小错误困住了。请帮帮我。
UPDATE :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)
答案 0 :(得分:1)
替换
getApplicationContext()
通过
this
必须使用作为活动的上下文而不是应用程序创建构建器。
答案 1 :(得分:0)
使用此代码:
AlertDialog ad = new AlertDialog.Builder(MainStart.this).create();
ad.setTitle("Error");
ad.setMessage("plz select the username and password");
ad.setButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
ad.show();
答案 2 :(得分:0)
而不是getApplicationContext()使用classname.this。它会帮助你