4

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();
7
  • 1
    Can you show more of your class? Specifically the declaration and definition of m_ClassContext? Commented Aug 17, 2012 at 17:25
  • is com.work.plan.Login$1.onClick(Login.java:64) one of your classes? whats happening in line 64? Is this the marked line? Commented Aug 17, 2012 at 17:29
  • can you post the manifest xml. maybe its related to theme Commented Aug 17, 2012 at 17:29
  • AlertDialog alert = new AlertDialog.Builder(getBaseContext()).create(); Commented Aug 17, 2012 at 17:31
  • this is my declaration of m_classContext: private Login m_ClassContext; where Login is my class name. Commented Aug 17, 2012 at 17:35

1 Answer 1

9

Instead of using m_ClassContext use:

//Please change NameOfYourActivity to what it should be
AlertDialog.Builder alert = new AlertDialog.Builder(NameOfYourActivity.this); 
Sign up to request clarification or add additional context in comments.

1 Comment

This was the solution however i created a 'Context' object called 'm_ClassContext' and set its value to the current context (i.e 'this')thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.