Note that there are some explanatory texts on larger screens.

plurals
  1. POAlertDialog in Android - Null Pointer Exception
    primarykey
    data
    text
    <p>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?</p> <pre><code>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.&lt;init&gt;(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) </code></pre> <p>And here is the class that is causing the error :</p> <pre><code>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 --&gt;** 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(); } } }; </code></pre> <p>I changed the alert to this </p> <pre><code>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(); </code></pre> <p>And now i get the following error: </p> <pre><code>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) </code></pre> <p>This ended up being the solution, Thanks guys</p> <pre><code>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(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload