0

这是我用 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)
4

3 回答 3

1

代替

getApplicationContext()

经过

this

必须使用作为活动而不是应用程序的上下文来创建构建器。

于 2012-02-14T13:21:45.360 回答
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();
于 2012-02-14T13:20:27.327 回答
0

而不是 getApplicationContext() 使用 classname.this。它会帮助你

于 2012-02-14T13:41:58.330 回答