当我尝试从Java启用设备侦听模块时,为什么我的Android Android反应本机应用会立即崩溃



我有一个Java模块,该模块检测到磁卡读取器是否将外围设置连接到Android手机上。它在完整的Android版本中正常工作,并显示出烤面包如果未连接外围,这是工作完整的本机Java代码:

public class MainActivity extends AppCompatActivity {
private SwipeHandler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    run();
}
public void run(){
    SwipeListener listener = new SwipeListener() {
        @Override
        public void onDisconnected(SwipeEvent swipeEvent) {
            Toast someToast = Toast.makeText(getApplicationContext(),"Card is disconnected!", Toast.LENGTH_LONG);
            someToast.show();
        }
        @Override
        public void onConnected(SwipeEvent swipeEvent) {
        }
        @Override
        public void onStarted(SwipeEvent swipeEvent) {
        }
        @Override
        public void onStopped(SwipeEvent swipeEvent) {
        }
        @Override
        public void onReadData(SwipeEvent swipeEvent) {
        }
        @Override
        public void onParseData(SwipeEvent swipeEvent) {
        }
        @Override
        public void onICDetected(SwipeEvent swipeEvent) {
        }
    };
    handler = new SwipeHandler(this);
    handler.addSwipeListener(listener);
    handler.setReadonly(true);
    handler.powerOn();
}

}

现在,在遵循来自React本地文档的模块桥接指令之后,我已经编写了下面的代码,将此功能获取到React Native应用程序中。模块代码:

public class mreaderManager extends ReactContextBaseJavaModule {
    private SwipeHandler handler;
    public mreaderManager(ReactApplicationContext reactContext){
        super(reactContext);
        run();
    }
    //override getName function
    @Override
    public String getName(){
        return "mreaderManager";
    }
    //function body
    @ReactMethod
    public void greetUser(String name, Callback callback){
        String greeting = "Welcome " + name;
        callback.invoke(greeting);
    }
    public void run(){
        //declare swipe listener
        SwipeListener listener = new SwipeListener() {
            @Override
            public void onDisconnected(SwipeEvent swipeEvent) {
                Toast someToast = Toast.makeText(getReactApplicationContext(),"Card is disconnected!", Toast.LENGTH_LONG);
                someToast.show();
            }
            @Override
            public void onConnected(SwipeEvent swipeEvent) {
            }
            @Override
            public void onStarted(SwipeEvent swipeEvent) {
            }
            @Override
            public void onStopped(SwipeEvent swipeEvent) {
            }
            @Override
            public void onReadData(SwipeEvent swipeEvent) {
            }
            @Override
            public void onParseData(SwipeEvent swipeEvent) {
            }
            @Override
            public void onICDetected(SwipeEvent swipeEvent) {
            }
        };
        //instantiate swipe handler
        handler = new SwipeHandler(getReactApplicationContext());
        handler.addSwipeListener(listener);
        handler.setReadonly(true);
        handler.powerOn();
    }
}

这是软件包代码:

public class mreaderPackage implements ReactPackage {
    @Override
    public List
            <Class<? extends JavaScriptModule>>
    createJSModules(){
        return Collections.emptyList();
    }
    @Override
    public List
            <ViewManager> createViewManagers(ReactApplicationContext reactContext){
        return Collections.emptyList();
    }
    @Override
    public List
            <NativeModule> createNativeModules(ReactApplicationContext reactContext){
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new mreaderManager(reactContext));
        return modules;
    }
}

React Native App一旦我实现此目的就会崩溃。我注意到当它实例化将上下文作为参数: handler = new SwipeHandler(getReactApplicationContext());实例化时,它会崩溃。我犯了什么错误?该应用具有所有必要的权限。我想提一下,在创建完全本地的Android版本时,该应用程序的行为方式相同,直到我停止使用" Instant Run"。

来自电话的碰撞日志(华为P8 Lite):

------ SYSTEM LOG (logcat -v threadtime -d *:v) ------ --------- beginning of crash 02-26 17:40:48.669 8024 8024 E AndroidRuntime: FATAL EXCEPTION: main 02-26 17:40:48.669 8024 8024 E AndroidRuntime: Process: com.zynletest, PID: 8024 02-26 17:40:48.669 8024 8024 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) } in com.imagpay.bP@5f836e9 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:972) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:743) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at android.os.Looper.loop(Looper.java:150) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5621) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from field 'boolean com.imagpay.bN.i' on a null object reference 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.imagpay.SwipeHandler.isReadable(SourceFile:655) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.imagpay.SwipeHandler.powerOff(SourceFile:510) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.imagpay.SwipeHandler.onDisconnected(SourceFile:571) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.imagpay.SwipeHandler.access$0(SourceFile:568) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at com.imagpay.bP.onReceive(SourceFile:1133) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:962) 02-26 17:40:48.669 8024 8024 E AndroidRuntime: ... 7 more 02-26 17:41:23.274 8348 8348 E AndroidRuntime: FATAL EXCEPTION: main 02-26 17:41:23.274 8348 8348 E AndroidRuntime: Process: com.zynletest, PID: 8348 02-26 17:41:23.274 8348 8348 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) } in com.imagpay.bP@ca08f6e 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:972) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:743) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at android.os.Looper.loop(Looper.java:150) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5621) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from field 'boolean com.imagpay.bN.i' on a null object reference 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.imagpay.SwipeHandler.isReadable(SourceFile:655) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.imagpay.SwipeHandler.powerOff(SourceFile:510) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.imagpay.SwipeHandler.onDisconnected(SourceFile:571) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.imagpay.SwipeHandler.access$0(SourceFile:568) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at com.imagpay.bP.onReceive(SourceFile:1133) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:962) 02-26 17:41:23.274 8348 8348 E AndroidRuntime: ... 7 more 02-26 17:42:08.267 8989 8989 E AndroidRuntime: FATAL EXCEPTION: main 02-26 17:42:08.267 8989 8989 E AndroidRuntime: Process: com.zynletest, PID: 8989 02-26 17:42:08.267 8989 8989 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) } in com.imagpay.bP@79b169c 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:972) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:743) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at android.os.Looper.loop(Looper.java:150) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5621) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from field 'boolean com.imagpay.bN.i' on a null object reference 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.imagpay.SwipeHandler.isReadable(SourceFile:655) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.imagpay.SwipeHandler.powerOff(SourceFile:510) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.imagpay.SwipeHandler.onDisconnected(SourceFile:571) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.imagpay.SwipeHandler.access$0(SourceFile:568) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at com.imagpay.bP.onReceive(SourceFile:1133) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:962) 02-26 17:42:08.267 8989 8989 E AndroidRuntime: ... 7 more 02-26 17:42:12.375 9074 9074 E AndroidRuntime: FATAL EXCEPTION: main 02-26 17:42:12.375 9074 9074 E AndroidRuntime: Process: com.zynletest, PID: 9074 02-26 17:42:12.375 9074 9074 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) } in com.imagpay.bP@79b169c 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:972) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:743) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at android.os.Looper.loop(Looper.java:150) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5621) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from field 'boolean com.imagpay.bN.i' on a null object reference 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.imagpay.SwipeHandler.isReadable(SourceFile:655) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.imagpay.SwipeHandler.powerOff(SourceFile:510) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.imagpay.SwipeHandler.onDisconnected(SourceFile:571) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.imagpay.SwipeHandler.access$0(SourceFile:568) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at com.imagpay.bP.onReceive(SourceFile:1133) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:962) 02-26 17:42:12.375 9074 9074 E AndroidRuntime: ... 7 more 02-26 17:42:40.481 9207 9207 E AndroidRuntime: FATAL EXCEPTION: main 02-26 17:42:40.481 9207 9207 E AndroidRuntime: Process: com.zynletest, PID: 9207 02-26 17:42:40.481 9207 9207 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) } in com.imagpay.bP@ca08f6e 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:972) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:743) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at android.os.Looper.loop(Looper.java:150) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5621) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from field 'boolean com.imagpay.bN.i' on a null object reference 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.imagpay.SwipeHandler.isReadable(SourceFile:655) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.imagpay.SwipeHandler.powerOff(SourceFile:510) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.imagpay.SwipeHandler.onDisconnected(SourceFile:571) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.imagpay.SwipeHandler.access$0(SourceFile:568) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at com.imagpay.bP.onReceive(SourceFile:1133) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:962) 02-26 17:42:40.481 9207 9207 E AndroidRuntime: ... 7 more

更新:

在考虑了更多情况之后,我注意到Imagpay库正在使用耳机插头来执行此操作,并且HeadSet_plug的意图是"粘性"。这意味着HEADET_PLUG意图可以在系统中徘徊,并在注册接收器后立即被应用程序接收。这可能解释了为什么这些事件在SwipeHander仍在构建自己时会产生错误。因此,解决此问题的可能方法(假设上述分析是正确的)是在创建新的SwipeHandler之前读取现有意图。这是一个可能起作用的示例(从这里取):

    ...
    // Register a receiver for a little while.
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    myReceiver = new IgnorantIntentReceiver();
    registerReceiver(myReceiver, filter);
    // Give it a moment to deliver the intents.
    Thread.sleep(300);
    unregisterReceiver(myReceiver);
    //instantiate swipe handler
    handler = new SwipeHandler(getReactApplicationContext());
    handler.addSwipeListener(listener);
    handler.setReadonly(true);
    handler.powerOn();
}
private class IgnorantIntentReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
        int state = intent.getIntExtra("state", -1);
        switch (state) {
        case 0:
            Log.d(TAG, "Headset is unplugged");
            break;
        case 1:
            Log.d(TAG, "Headset is plugged");
            break;
        default:
            Log.d(TAG, "I have no idea what the headset state is");
        }
    }
}
}

注意:我认为,也许应该将创建,注册和未注册转移到活动的onstart()和onStop()生命周期方法。请参阅此处。


从logcat中,由于在SwipeHandler类中接收NullPoInterException引起的错误。您尚未发布SwipeHandler类的代码,因此我无法进一步诊断。问题正在这里发生:

com.imagpay.SwipeHandler.isReadable(SourceFile:655)

它正在尝试从Israble方法中的空对象中弄清状态。

可能是开发出来用于在Android应用中使用的,而不是特别是React Native应用程序。在这种情况下,问题可能源于您在每种情况下创建SwipeHandler的方式。代码上方有2个位置,您可以在其中初始化SwipeHandler,其中一个是针对React Native版本:

handler = new SwipeHandler(getReactApplicationContext());

也许您可以尝试将其引用对实际的Android应用程序上下文。类似:

 handler = new SwipeHandler(getReactApplicationContext().getBaseContext());

相关内容

  • 没有找到相关文章

最新更新