Xamarin SIGSEGV 在注册接收器时



尝试运行应用程序,在我的 MainActivity 类 OnCreate 方法中,我有以下内容:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    bcastRx = new BroadcastRx();
    bcastRx.Received += HandleOnBroadcastReceive;
    RegisterReceiver (bcastRx, new IntentFilter("hello"));
    ...

接收器类如下所示:

[BroadcastReceiver]
[Register("android/content/BroadcastReceiver", DoNotGenerateAcw=true)]
class BroadcastRx : BroadcastReceiver{
    public delegate void Receive(Context c, Intent intent);
    public event Receive Received;
    public override void OnReceive(Context c, Intent intent) {
        if(Received != null)
            Received(c,intent);
    }
}

在调用RegisterReceiver的线路上,我在Nexus 5上得到了一个SIGSEGV(但不是在模拟器上)。回溯:

[mono-rt]   at <unknown> <0xffffffff>
[mono-rt]   at (wrapper managed-to-native) object.wrapper_native_0xb4db2c65 (intptr,intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x0012b, 0xffffffff>
[mono-rt]   at (wrapper delegate-invoke) <Module>.invoke_intptr_intptr_intptr_intptr_intptr_JValue[] (intptr,intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x00069, 0xffffffff>
[mono-rt]   at Android.Runtime.JNIEnv.CallNonvirtualObjectMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00060] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:542
[mono-rt]   at Android.Content.ContextWrapper.RegisterReceiver (Android.Content.BroadcastReceiver,Android.Content.IntentFilter) [0x00089] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Content.ContextWrapper.cs:2169
[mono-rt]   at client.Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00038] in c:WorkxamarinclientAndroidMainActivity.cs:31
[mono-rt]   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2380
[mono-rt]   at (wrapper dynamic-method) object.47ba580a-bf7b-4507-8c4d-53e613b2dc06 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
[mono-rt]   at (wrapper native-to-managed) object.47ba580a-bf7b-4507-8c4d-53e613b2dc06 (intptr,intptr,intptr) <IL 0x00023, 0xffffffff>
[mono-rt] 
[mono-rt] =================================================================
[mono-rt] Got a SIGSEGV while executing native code. This usually indicates
[mono-rt] a fatal error in the mono runtime or one of the native libraries 
[mono-rt] used by your application.
[mono-rt] =================================================================
[mono-rt] 
[libc] Fatal signal 11 (SIGSEGV), code 1, fault addr 0x3e in tid 29973 (m.test.client)

关于我可能做错了什么的任何想法??

编辑我试过:

  • 传递空意图过滤器,结果相同
  • 向我的安卓清单添加大量权限
  • 将IntentFilter 构造函数和 RegisterReceiver 拆分到不同的行上。问题绝对是RegisterReceiver

好吧,我终于找到了; 反复试验,所以我无法解释为什么这是正确的答案。

[BroadcastReceiver]
// remove this -> [Register("android/content/BroadcastReceiver", DoNotGenerateAcw=true)]
class BroadcastRx : BroadcastReceiver{
    public delegate void Receive(Context c, Intent intent);
    public event Receive Received;
    public override void OnReceive(Context c, Intent intent) {
        if(Received != null)
            Received(c,intent);
    }
}

魔法。

最新更新