[英]Why does my Android react native app crash instantly when I try to bridge device listening modules from Java
I have a java module that detects if a magnetic card reader peripheral is attached to an android phone or not. 我有一个java模块,可以检测磁卡读卡器外围设备是否连接到Android手机。 it works fine in the full android version and shows a toast if the peripheral is not connected, this is the working full native java code: 它在完整的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();
}
} }
Now, after following the module bridging instructions from the react native docs I have written the code below to get this feature into a react native app. 现在,在遵循来自react本机文档的模块桥接指令后,我已经编写了下面的代码,以将此功能添加到react本机应用程序中。 The module code: 模块代码:
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();
}
}
and here is the package code: 这是包裹代码:
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;
}
}
The react native app crashes as soon as it opens once I implement this. 一旦我实现了这个,本机应用程序就会在打开后立即崩溃。 I noticed it crashes when it instantiates the SwipeHandler that takes context as an argument: handler = new SwipeHandler(getReactApplicationContext());
我注意到它在实例化以上下文为参数的SwipeHandler时崩溃: handler = new SwipeHandler(getReactApplicationContext());
. 。 What mistake am I making? 我犯了什么错误? The app has all the necessary permissions. 该应用程序具有所有必要的权限。 I would like to mention that when creating the fully native android version, the app was behaving the same way till I stopped using 'Instant Run'. 我想提一下,在创建完全原生的Android版本时,应用程序的行为方式相同,直到我停止使用“即时运行”。
Crash log from phone(Huawei P8 Lite): 手机崩溃日志(华为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
UPDATE: 更新:
After pondering the situation some more, I notice that the imagpay library is using the headphone plug to do it's thing and that the HEADSET_PLUG intent is "sticky". 在仔细思考了这种情况之后,我注意到imagpay库正在使用耳机插头来做它的事情并且HEADSET_PLUG意图是“粘性的”。 This means HEADSET_PLUG intents can hang around in the system and be received by an application as soon as it registers a receiver. 这意味着HEADSET_PLUG意图可以在系统中闲逛,并在注册接收器后立即被应用程序接收。 This might explain why these events are generating errors if they turn up while the SwipeHander is still constructing itself. 这可以解释为什么这些事件在SwipeHander仍在构建自身时出现错误。 So a possible approach to solve this (assuming the above analysis is right) is to read off the existing intents before creating the new SwipeHandler. 因此,解决此问题的可能方法(假设上述分析是正确的)是在创建新的SwipeHandler之前读取现有的意图。 Here is an example that might work (taken from here ): 这是一个可行的例子(取自这里 ):
...
// 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");
}
}
}
}
NOTE: I think the creation, registering and unregistering should perhaps be moved to the onStart() and onStop() lifecycle methods of the Activity. 注意:我认为创建,注册和取消注册可能应该移动到Activity的onStart()和onStop()生命周期方法。 See here . 看到这里 。
From the logcat, the error is being caused by receiving a NullPointerException in your SwipeHandler class. 从logcat中,错误是由SwipeHandler类中的NullPointerException接收引起的。 You have not posted the code for the SwipeHandler class, so I cannot diagnose further. 您尚未发布SwipeHandler类的代码,因此无法进一步诊断。 The issue is happening here: 这个问题发生在这里:
com.imagpay.SwipeHandler.isReadable(SourceFile:655)
It is trying to work out the state from a null object within the isReadable method. 它试图从isReadable方法中的null对象计算出状态。
It could be that the imagpay library was developed to be used in an Android application and not specifically a React Native application. 可能是imagpay库被开发用于Android应用程序而不是React Native应用程序。 In this case, the problem may stem from the way you are creating the SwipeHandler in each case. 在这种情况下,问题可能源于您在每种情况下创建SwipeHandler的方式。 There are 2 places in the code above where you initialize a SwipeHandler, one of them is for the React Native version: 上面的代码中有2个位置用于初始化SwipeHandler,其中一个用于React Native版本:
handler = new SwipeHandler(getReactApplicationContext());
Perhaps you could try to give it a reference to the actual Android application context. 也许你可以尝试给它一个实际的Android应用程序上下文的引用。 Something like: 就像是:
handler = new SwipeHandler(getReactApplicationContext().getBaseContext());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.