2012-02-07 41 views
4

我正在使用C2DM和PhoneGap一起使用。当我收到一条C2DM消息时,我会显示一条通知(通过NotificationManager)。当用户选择通知时,我的应用程序会收到一个意图。在这种情况下,我想在我的jquery-mobile webapp中激活一个页面。Phonegap和C2DM - 空指针异常

所以我推翻了onNewIntent事件存储意图:

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

然后,在onResume我激活了正确的页面,如果意图是从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"); 
    } 
} 

这工作不错,但它有时会出现NullPointerException异常 - 不是在我的手机或模拟器上,而是在其他设备上。堆栈跟踪说,这是在DroidGap活动onNewIntent,看到Code

protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    //Forward to plugins 
    this.pluginManager.onNewIntent(intent); 
} 

我无法重现这种情况。很显然,pluginManager是空的,但我不明白为什么。

所以问题是:

  • 是拍摄方式从Android好选择jQuery的移动特定页面或可有人指出一个更好的办法?
  • 我该如何摆脱这个异常?当然,我可以检查pluginManager是否为空,并且在这种情况下不要调用super - 但是然后我的特定页面未被激活。
  • 你认为这是一个PhoneGap错误 - 不检查pluginmanager为null吗?当我检查PhoneGap代码时,我认为这绝不应该发生,在我的理解中,onNewIntent只在加载活动时调用,否则将触发onCreate。

更新 我现在更懂得:在没有加载应用程序和C2DM消息到达出现问题。 意图启动应用程序,该事件发生在下列顺序 - 但onNewIntent只是偶尔叫:每当onNewIntent崩溃启动期间执行

onCreate() 
onNewIntent() 
onResume() 

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

当我想改变的开始页面中的onResume-事件,这并不PhoneGap的时候还没有准备好工作:反正我解决了这个问题。因此,只要在应用程序启动的情况下尽早调用onResume中的#messages页面即可。但是什么时候打电话呢?是否有可能挂钩onDeviceReady?

回答

1

我仍然不知道为什么有时onNewIntent会在应用程序启动时触发(不仅仅是激活),有时候不会。无论如何,我用一些解决方法解决了我所有的问题。

在我的活动我创建了一个新的功能(不相关的部分被剥离):

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

    // activate onResume instead 
    isReady = true; 
} 

及以上布尔标志:

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

我激活onCreate事件的回调:

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

并从Javascript调用它onDeviceReady

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

在我用的是协商逻辑的onResume事件:

protected void onResume() 
{ 
    super.onResume(); 

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

这保证了页面选择只执行一次,无论是在的onResume或onDeviceReady。