2014-09-28 69 views
0

我使用Xamarin Android開發Android應用程序。Xamarin Android不幸的應用程序已停止

我試圖通過

AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) => 
            { 
               //WRITE TO FILE 
            }; 
            AppDomain.CurrentDomain.UnhandledException += (s, e) => 
            { 
                /* 
                 * When a background thread crashes this is the code that will be executed. You can 
                 * recover from this. 
                 */ 
            }; 

趕上全球例外我仍然收到「不幸的是應用程序已經停止的錯誤」頻頻在各種設備上。錯誤沒有被捕獲。

我懷疑這是內存泄漏,但我不確定。我無法訪問經常出現此錯誤的設備的logcat文件。我在我的智慧結束。任何人都可以點亮一下嗎?

在一個側面說明,如果一個全球性的異常被引發和捕獲,UnhandledExceptionRaiser在之間中斷,不寫入文件(這我猜是因爲Android的清理過程?)

+0

請張貼的logcat。 – 2014-09-29 11:31:34

+0

@MysticMagic你甚至看這個問題? – 2014-09-29 14:03:32

+0

是的,我讀,我讀了,太「的錯誤不是c 「但那不會幫助我們幫助你..所以任何方式,祝你好運。 :) – 2014-09-30 04:19:28

回答

3

在分析Google崩潰報告後,我發現了實際的錯誤。

11-12 22:36:31.410: E/AndroidRuntime(4835): FATAL EXCEPTION: main 
11-12 22:36:31.410: E/AndroidRuntime(4835): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.apptimator.pro.mayusha/com.apptimator.mayusha.HomeActivity}: java.lang.RuntimeException: Parcel [email protected]: Unmarshalling unknown type code 7209057 at offset 1064 

...... 
11-12 22:36:31.410: E/AndroidRuntime(4835): Caused by: java.lang.RuntimeException: Parcel [email protected]: Unmarshalling unknown type code 7209057 at offset 1064 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.os.Parcel.readValue(Parcel.java:2038) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.os.Parcel.readMapInternal(Parcel.java:2255) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.os.Bundle.unparcel(Bundle.java:223) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.os.Bundle.getSparseParcelableArray(Bundle.java:1237) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1969) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.app.Activity.onRestoreInstanceState(Activity.java:977) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.app.Activity.performRestoreInstanceState(Activity.java:949) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1155) 
11-12 22:36:31.410: E/AndroidRuntime(4835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271) 

這個錯誤可以通過this ("Proguard Causing Runtime exception")或在此Xamarin的Android的情況下造成的,由Android未能恢復保存的實例狀態,由於嵌套的保存狀態的實現。

我用SlidingMenuSharp(SlidingMenuSharp link)庫在其中執行IParcelable並使用嵌套SavedState類家庭活動。可以發現同樣的錯誤here

不過,我設法解決它和它的現在都工作正常。要修復錯誤,請按照SavedState,OnRestoreInstanceState的onSaveInstanceState和所提到的捆綁實現

public class SavedState : BaseSavedState 
    { 
     public int Item { get; private set; } 

     public SavedState(IParcelable superState) 
      : base(superState) 
     { 

     } 

     public SavedState(Parcel parcel) 
      : base(parcel) 
     { 
      Item = parcel.ReadInt(); 
     } 

     public override void WriteToParcel(Parcel dest, ParcelableWriteFlags flags) 
     { 
      base.WriteToParcel(dest, flags); 
      dest.WriteInt(Item); 
     } 

     [ExportField("CREATOR")] 
     static SavedStateCreator InitializeCreator() 
     { 
      return new SavedStateCreator(); 
     } 

     class SavedStateCreator : Java.Lang.Object, IParcelableCreator 
     { 
      public Java.Lang.Object CreateFromParcel(Parcel source) 
      { 
       return new SavedState(source); 
      } 

      public Java.Lang.Object[] NewArray(int size) 
      { 
       return new SavedState[size]; 
      } 
     } 
    } 

    protected override void OnRestoreInstanceState(IParcelable state) 
    { 
     try 
     { 
      Bundle bundle = state as Bundle; 
      if (bundle != null) 
      { 
       IParcelable superState = (IParcelable)bundle.GetParcelable("base"); 
       if (superState != null) 
        base.OnRestoreInstanceState(superState); 
       _viewAbove.SetCurrentItem(bundle.GetInt("currentPosition", 0)); 
      } 
     } 
     catch 
     { 
      base.OnRestoreInstanceState(state); 
      // Ignore, this needs to support IParcelable... 
     } 
    } 

    protected override IParcelable OnSaveInstanceState() 
    { 
     var superState = base.OnSaveInstanceState(); 
     Bundle state = new Bundle(); 
     state.PutParcelable("base", superState); 
     state.PutInt("currentPosition", _viewAbove.GetCurrentItem()); 

     return state; 
    } 
+0

這也適用於來自Xamarin示例的CirclePageIndicator--它們在「Unmarshalling unknown type code」上實現OnRestoreInstanceState和OnSaveInstanceState崩潰,您的方法可行 – rouen 2015-03-19 12:48:17

+0

@rouen請向他們報告/創建請求:) – 2015-04-07 04:51:24

+0

查看此解決方案後,我意識到我應該重新啓動設備,這爲我解決了問題。不知道這是否是同一個問題。 – 2015-09-09 00:53:08

0

你應該不要使用AppDomain.CurrentDomain.UnhandledException的辦法攔截未處理的異常,如Xamarin docs提到:

注意:您可以不依賴AppDomain.UnhandledException事件作爲 管理例外情況MonoDroid的從來未處理的;他們總是在攔截(異常) 區塊內的Android /管理邊界處截獲 。

我建議繼續使用AndroidEnvironment.UnhandledExceptionRaiser,並儘可能保持操作效率。

也可以使用TaskScheduler.UnobservedTaskException捕獲後臺線程中發生並且未發現異常(並且不會導致未處理的異常)的異常。

最後,我訂閱了我的應用程序類(從Android.App.Application繼承的類)中的所有異常處理事件,因爲它是應用程序的第一個入口點,甚至在主要活動開始(或從現在開始訂閱的任何地方)

PS我也使用TaskScheduler.UnobservedTaskException只是爲了安全起見,但從來沒有發現任何異常。

+0

我在我的Application類中使用AndroidEnvironment.UnhandledExceptionRaiser。我剛剛放入AppDomain.CurrentDomain.UnhandledException以保證安全。 – 2014-09-29 14:04:24

相關問題