Reputation: 9371
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();
}
Upvotes: 0
Views: 305
Reputation: 244
Check your context - (StartActivity.this) in
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(StartActivity.this);
Perhaps Context is null;
Upvotes: 1