Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog in Android - Null Pointer Exception

I looked online for quite a bit and am now getting frustrated cause I can't find information on the following error. Can someone please help me figure out whats going on with this?

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)

And here is the class that is causing the error :

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();
            }
        }

    };

I changed the alert to this

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();

And now i get the following error:

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)

This ended up being the solution, Thanks guys

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();
like image 537
kandroidj Avatar asked Jan 15 '23 19:01

kandroidj


1 Answers

Instead of using m_ClassContext use:

//Please change NameOfYourActivity to what it should be
AlertDialog.Builder alert = new AlertDialog.Builder(NameOfYourActivity.this); 
like image 87
Vishwa Patel Avatar answered Jan 19 '23 00:01

Vishwa Patel