Phonegap and C2DM - NullPointerException

I use C2DM with PhoneGap. When I receive a C2DM message, I show a notification (via NotificationManager ). When a user selects a notification, my application receives the intention. In this case, I want to activate the page in my jquery-mobile webapp.

So I flipped the onNewIntent event to keep the intent:

 @Override protected void onNewIntent(final Intent intent) { super.onNewIntent(intent); setIntent(intent); } 

Then in onResume I activated the correct page if the target was from C2DM:

 @Override protected void onResume() { super.onResume(); // read possible argument boolean showMessage = getIntent().getBooleanExtra(ARG_SHOW_MESSAGES, false); if (showMessage) { clearNotification(); super.loadUrl("file:///android_asset/www/de/index.html#messages"); } } 

This works great, but sometimes it crashes with a NullPointerException - not on my mobile device or emulator, but on other devices. The stack stack says it is in the onNewIntent activity, see Code :

 protected void onNewIntent(Intent intent) { super.onNewIntent(intent); //Forward to plugins this.pluginManager.onNewIntent(intent); } 

I could not reproduce this situation. Obviously pluginManager is NULL, but I don't know why.

So the questions are:

  • Is the approach suitable for selecting a specific page in jquery-mobile with Android, or can someone suggest a better approach?
  • How can I get rid of an exception? Of course, I can check the pluginManager for the absence of a zero value and in this case do not call it super - but then my specific page is not activated.
  • Do you think this is PhoneGap error - not checking pluginmanager for null? When I checked the PhoneGap code, I thought that this should never happen, in my understanding, onNewIntent is only called when activity is loaded, otherwise the onCreate action will be activated.

Update Now I know more: the problem occurs when the application does not load and the C2DM message appears. The goal begins with the application, events occur in the following order - but onNewIntent is called only occasionally:

 onCreate() onNewIntent() onResume() 

Whenever onNewIntent is executed during startup, it fails. Anyway, I fixed it with

 @Override protected void onNewIntent(final Intent intent) { // avoid Phonegap bug if (pluginManager != null) { super.onNewIntent(intent); } setIntent(intent); } 

When I want to change the start page in onResume-Event, this does not work if Phonegap is not ready. Thus, it is only too early to call the #messages page in onResume in the case when the application starts. But when to call? Is it possible to connect to onDeviceReady?

+4
source share
1 answer

I still don’t know why sometimes onNewIntent is launched at application startup time (it’s not just activated), and sometimes not. In any case, I solved all my problems with some workarounds.

In my activity, I created a new function (the corresponding parts are not separated):

 public void onDeviceReady() { if (!isReady) { super.loadUrl("file:///android_asset/www/en/index.html#messages"); } // activate onResume instead isReady = true; } 

and the boolean flag above:

 /** Is PhoneGap ready? */ private boolean isReady = false; 

I activate the callback in the onCreate event:

 // Callback setzen appView.addJavascriptInterface(this, "Android"); 

and name it from Javascript onDeviceReady

 if (OSName == "Android") { window.Android.onDeviceReady(); } 

In the onResume event, I use consistent logic:

 protected void onResume() { super.onResume(); if (isReady) { super.loadUrl("file:///android_asset/www/en/index.html#messages"); } } 

This ensures that page selection is performed only once, either in onResume or onDeviceReady.

+1
source

Source: https://habr.com/ru/post/1395223/


All Articles