2012-02-07 27 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。