我收到一個(gè)錯(cuò)誤,我無(wú)法理解: 我在void方法中有這個(gè)簡(jiǎn)單的警告對(duì)話框構(gòu)建器 private void startAction() {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder (this);
var ad = builder.Create ();
builder.SetMessage ("Some text");
builder.SetPositiveButton ("OK", delegate {
ad.Dismiss ();
ShowDialog (0);
});
builder.SetNegativeButton ("Cancel", delegate {
ad.Cancel ();
});
builder.SetCancelable (true);
builder.Show ();
}
Xamarin Insights向我展示了一份我不能復(fù)制或理解的崩潰報(bào)告(多次). System.ArgumentException'jobject' must not be IntPtr.Zero. Parameter name: jobject
Raw
Android.Runtime.JNIEnv.CallVoidMethod(IntPtr jobject, IntPtr jmethod)
Android.App.Dialog.Dismiss()
SpoonacularApp.Droid.ShoppingListActivity.<startAction>c__AnonStorey3.<>m__0(object, DialogClickEventArgs)
Android.Content.IDialogInterfaceOnClickListenerImplementor.OnClick(IDialogInterface dialog, int which)
Android.Content.IDialogInterfaceOnClickListenerInvoker.n_OnClick_Landroid_content_DialogInterface_I(IntPtr jnienv, IntPtr native__this, IntPtr native_dialog, int which)
at (wrapper dynamic-method) System.Object:ba5962df-899a-46fd-a4bd-6c9ffe426b75 (intptr,intptr,intptr,int)
這個(gè)例外談?wù)摰氖悄膫€(gè)論點(diǎn)? 我得到了與Android.App.Dialog.Cancel()而不是Android.App.Dialog.Dismiss()相同的錯(cuò)誤消息. 解決方法: AlertDialog廣告的peer connection;已被切斷;雖然該對(duì)象仍然可以在.NET中使用(通過(guò)點(diǎn)擊處理程序),但它已經(jīng)收集了Java對(duì)應(yīng)物.這兩個(gè)對(duì)象之間的綁定保存在存儲(chǔ)在IntPtr Handle屬性中的全局引用中,用于實(shí)現(xiàn)IJavaObject的.NET中的所有對(duì)象. 當(dāng)發(fā)生跨VM收集周期時(shí),Handle設(shè)置為IntPtr.Zero并釋放全局Java引用以使Dalvik(Java)能夠收集Java對(duì)象. 您看到此次崩潰是因?yàn)樵搼?yīng)用可能已經(jīng)落后,并且Android已觸發(fā)了應(yīng)用流程的集合.這導(dǎo)致大多數(shù)Java資源被Dalviks垃圾收集器破壞,但它們相應(yīng)的.NET對(duì)象仍處于活動(dòng)狀態(tài),現(xiàn)在指向無(wú)效的Java對(duì)象. 解決此問(wèn)題的方法是使用following code snippet檢查AlertDialog的單擊處理程序內(nèi)的對(duì)等連接: public static class PeerConnectionHelper
{
public static bool HasPeerConnection(Java.Lang.Object jObj)
{
return !(jObj == null || jObj.Handle == System.IntPtr.Zero);
}
public static bool HasPeerConnection (Android.Runtime.IJavaObject jObj)
{
return !(jObj == null || jObj.Handle == System.IntPtr.Zero);
}
}
這將實(shí)現(xiàn)如下: builder.SetPositiveButton ("OK", delegate {
if (!PeerConnectionHelper.HasPeerConnection(ad)) {
return;
}
ad.Dismiss ();
ShowDialog (0);
});
builder.SetNegativeButton ("Cancel", delegate {
if (!PeerConnectionHelper.HasPeerConnection(ad)) {
return;
}
ad.Cancel ();
});
|