0

i put an alert dialog inside a custom listener that i created, it is like a onClick listener. the listener is called when an event occurs in another class.

why the null pointer when i put the alert dialog code inside of the listener call back method? how can i fix this? and more importantly. why do i get the null pointer?

the listener calls the callback method onResultReturned in this Android java class, and when that happens i wanted the alert dialog to appear.

the strange thing is that this alert dialog code works fine outside of my callback method, like when i put it in the onCreate method

the activity i am in is StartActivity and the method onReturnResult is in this class, the other activity called, Synchronizer is where the interface for the listener is located.

stack trace;

09-27 16:08:42.300: E/AndroidRuntime(7195): FATAL EXCEPTION: main
09-27 16:08:42.300: E/AndroidRuntime(7195): java.lang.NullPointerException
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at .StartActivity.onResultReturned(StartActivity.java:100)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at .Synchronizer$SendOutMsgAndPack$2.run(Synchronizer.java:159)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.os.Handler.handleCallback(Handler.java:605)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.os.Looper.loop(Looper.java:137)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at java.lang.reflect.Method.invokeNative(Native Method)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at java.lang.reflect.Method.invoke(Method.java:511)
09-27 16:08:42.300: E/AndroidRuntime(7195):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

code for my onResultReturned callback method, which is called about 4 to 5 seconds after the activity is created usually

    @Override
public void onResultReturned(int result) {

     // build allertdialog
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(StartActivity.this);


     // set title
     alertDialogBuilder.setTitle("update status"); 
    //set allert message
     alertDialogBuilder
     .setMessage("update success")
     .setCancelable(false)
     .setPositiveButton("OK",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, close
                // current activity
                //StartActivity.this.finish();
            }
          });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

}

4
  • which line is line 100 in StartActivity ? Commented Sep 27, 2013 at 7:16
  • Intent myIntent = new Intent(WorkTypeSelection.this, SettingActivity.class);, that can't be the line you are looking for, i will have to post another stack trace as is slightly changes my code in the last few minutes, but line 100 is in the general area of some onClick listeners that are unrelated to the alert dialog code. Commented Sep 27, 2013 at 7:20
  • try to use getAppicationContext() instead of StartActivity.this Commented Sep 27, 2013 at 7:23
  • from the stack you provided : at .StartActivity.onResultReturned(StartActivity.java:100), this tells that the null pointer is in line 100, so check line 100 (you gotta learn to read the logcat, nullpointer exceptions are the easiest exceptions to debug) Commented Sep 27, 2013 at 7:23

1 Answer 1

1

Check your context - (StartActivity.this) in

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(StartActivity.this);

Perhaps Context is null;

Sign up to request clarification or add additional context in comments.

11 Comments

changed it to StartActivity.this and still getting the crashes
No, I mean put log there and check it, something like this: Log.e(getClass().getSimpleName(), "onResultReturned, Context: " + StartActivity.this );
it crashes right when the onResultReturned method is called, but does not crash if i remove all the code from inside the method. used a Log cat to check that the onResultReturned method has no errors. so there is probably somethign in the allert dialog code that is making it crash
checked it and StartActivity.this is not returning null inside of the onResultREturned method. i get @410dcba8 as a value
so this is getting strange, context is not null and the onResultReturned method is being called normally about 5 seconds after the activity starts but that does not explain why the code for the alert dialog would crash inside of this method when it works inside of the onCreate method. they are both callback methods, one of the strangest things i have ever seen
|

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.