我在网上看了很久,现在感到很沮丧,因为我找不到有关以下错误的信息。有人可以帮我弄清楚这是怎么回事吗?

08-17 13:15:47.800: W/dalvikvm(10356): threadid=1: thread exiting with uncaught exception (group=0x401f0760)
08-17 13:15:47.800: E/AndroidRuntime(10356): FATAL EXCEPTION: main
08-17 13:15:47.800: E/AndroidRuntime(10356): java.lang.NullPointerException
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:120)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:337)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at com.work.plan.Login$1.onClick(Login.java:64)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.view.View.performClick(View.java:3112)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.view.View$PerformClick.run(View.java:11956)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.os.Handler.handleCallback(Handler.java:587)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.os.Looper.loop(Looper.java:132)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at android.app.ActivityThread.main(ActivityThread.java:4025)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at java.lang.reflect.Method.invoke(Method.java:491)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-17 13:15:47.800: E/AndroidRuntime(10356):    at dalvik.system.NativeStart.main(Native Method)

这是导致错误的类:
OnClickListener LoginListener = new OnClickListener(){

        public void onClick(View arg0) {
            String checkUserName = userName.getText().toString();


            Cursor mCursor = mDbHelper.fetchUserByName(checkUserName);

            if(mCursor.moveToFirst()){
                String checkDatabaseUser = mCursor.getString(mCursor.getColumnIndexOrThrow(LoginDbAdapter.KEY_NAME));
                Toast.makeText(getBaseContext(), checkDatabaseUser,Toast.LENGTH_LONG).show();
                if(checkDatabaseUser.equals(checkUserName)){
                    Intent i = new Intent();
                    i.setClass(getBaseContext(), WorkoutPlanActivity.class);
                    startActivity(i);
                }
            }else {
        **//Error happening here -->**  AlertDialog.Builder alert = new AlertDialog.Builder(m_ClassContext);
                    alert.setTitle("Error Logging in");
                    alert.setMessage("The user name is not in the system.");
                    alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.cancel();
                        }
                    });
                    alert.show();
            }
        }

    };

我将警报更改为此
AlertDialog alert = new AlertDialog.Builder(getBaseContext()).create();
                alert.setTitle("Error Logging in");
                alert.setMessage("The user name is not in the system.");
                alert.setButton("Ok",new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                });
                alert.show();

现在我收到以下错误:
08-17 13:38:38.460: W/dalvikvm(10513): threadid=1: thread exiting with uncaught exception (group=0x401f0760)
08-17 13:38:38.460: E/AndroidRuntime(10513): FATAL EXCEPTION: main
08-17 13:38:38.460: E/AndroidRuntime(10513): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.view.ViewRoot.setView(ViewRoot.java:450)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.app.Dialog.show(Dialog.java:269)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at com.work.plan.Login$1.onClick(Login.java:74)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.view.View.performClick(View.java:3112)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.view.View$PerformClick.run(View.java:11956)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.os.Handler.handleCallback(Handler.java:587)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.os.Looper.loop(Looper.java:132)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at android.app.ActivityThread.main(ActivityThread.java:4025)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at java.lang.reflect.Method.invoke(Method.java:491)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-17 13:38:38.460: E/AndroidRuntime(10513):    at dalvik.system.NativeStart.main(Native Method)

这最终成为解决方案,谢谢大家
private Context m_ClassContext = this;

AlertDialog alert = new AlertDialog.Builder(m_ClassContext).create();
                alert.setTitle("Error Logging in");
                alert.setMessage("The user name is not in the system.");
                alert.setButton("Ok",new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                });
                alert.show();

最佳答案

而不是使用 m_ClassContext 使用:

//Please change NameOfYourActivity to what it should be
AlertDialog.Builder alert = new AlertDialog.Builder(NameOfYourActivity.this);

关于android - Android 中的 AlertDialog - 空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12010324/

10-11 10:39