Xamarin Android不幸的应用程序已停止
我使用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的清理过程?)
在分析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;
}
这也适用于来自Xamarin示例的CirclePageIndicator--它们在“Unmarshalling unknown type code”上实现OnRestoreInstanceState和OnSaveInstanceState崩溃,您的方法可行 – rouen 2015-03-19 12:48:17
@rouen请向他们报告/创建请求:) – 2015-04-07 04:51:24
查看此解决方案后,我意识到我应该重新启动设备,这为我解决了问题。不知道这是否是同一个问题。 – 2015-09-09 00:53:08
你应该不要使用AppDomain.CurrentDomain.UnhandledException
的办法拦截未处理的异常,如Xamarin docs提到:
注意:您可以不依赖AppDomain.UnhandledException事件作为 管理例外情况MonoDroid的从来未处理的;他们总是在拦截(异常) 区块内的Android /管理边界处截获 。
我建议继续使用AndroidEnvironment.UnhandledExceptionRaiser
,并尽可能保持操作效率。
也可以使用TaskScheduler.UnobservedTaskException
捕获后台线程中发生并且未发现异常(并且不会导致未处理的异常)的异常。
最后,我订阅了我的应用程序类(从Android.App.Application继承的类)中的所有异常处理事件,因为它是应用程序的第一个入口点,甚至在主要活动开始(或从现在开始订阅的任何地方)
PS我也使用TaskScheduler.UnobservedTaskException
只是为了安全起见,但从来没有发现任何异常。
我在我的Application类中使用AndroidEnvironment.UnhandledExceptionRaiser。我刚刚放入AppDomain.CurrentDomain.UnhandledException以保证安全。 – 2014-09-29 14:04:24
请张贴的logcat。 – 2014-09-29 11:31:34
@MysticMagic你甚至看这个问题? – 2014-09-29 14:03:32
是的,我读,我读了,太“的错误不是c “但那不会帮助我们帮助你..所以任何方式,祝你好运。 :) – 2014-09-30 04:19:28