Reputation: 127
Hello I have some trouble while trying to run an alert in my AsyncTask. Whenever something with context is run, the app crashes, like trying to let alert appear.
I am following this tutorial: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
I am sure, that the error is because of a wrong "context" or something like that.
I am calling my AsyncTask class from the GCM class like this:
GCM.java:
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
// displayMessage(context, message);
// notifies user
generateNotification(context, message);
runAsync(context);
// update database
}
// here I want to run my asyncTask
private void runAsync(Context context){
TestActivity = new TestActivity(context);
task.execute();
}
TestActivity.java:
Context mContext;
public TestActivity(Context context) {
this.mContext = context;
}
// Progress Dialog
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
productsList = new ArrayList<HashMap<String, String>>();
pDialog = new ProgressDialog(mContext);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// here when It shall show the alert it crashes... with the following error
pDialog.show();
}
F/AndroidRuntime(19976): FATAL EXCEPTION: IntentService[GCM-980252920256-1]
F/AndroidRuntime(19976): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
F/AndroidRuntime(19976): at android.view.ViewRootImpl.setView(ViewRootImpl.java:792)
F/AndroidRuntime(19976): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265)
F/AndroidRuntime(19976): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
F/AndroidRuntime(19976): at android.app.Dialog.show(Dialog.java:282)
F/AndroidRuntime(19976): at com.test.app.TestActivity.onPreExecute(TestActivity.java:72)
F/AndroidRuntime(19976): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
F/AndroidRuntime(19976): at android.os.AsyncTask.execute(AsyncTask.java:534)
F/AndroidRuntime(19976): at com.test.app.GCM.asyncTaskRun(GCM.java:63)
F/AndroidRuntime(19976): at com.test.app.GCM.onMessage(GCM.java:55)
F/AndroidRuntime(19976): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
F/AndroidRuntime(19976): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
F/AndroidRuntime(19976): at android.os.Handler.dispatchMessage(Handler.java:99)
F/AndroidRuntime(19976): at android.os.Looper.loop(Looper.java:176)
F/AndroidRuntime(19976): at android.os.HandlerThread.run(HandlerThread.java:60)
Where is my error?
Upvotes: 0
Views: 246
Reputation: 1210
ProgressDialog needs an Activity as Context (that's a bad design in Android, Context is not always the required Context).
You should open an Activity that looks like a dialog:
See Android Activity as a dialog
Upvotes: 1
Reputation: 2075
Pass context as an Activity
not as an ApplicationContext
. Both classes extends Context
class.
Upvotes: 0