gpt4 book ai didi

java - 当我尝试从 Java 桥接设备监听模块时,为什么我的 Android 会立即响应 native 应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:07 25 4
gpt4 key购买 nike

我有一个 java 模块,可以检测磁卡读卡器外围设备是否连接到 android 手机。它在完整的 android 版本中运行良好,如果未连接外围设备,它会显示一个 toast,这是工作的完整 native 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 native 文档中的模块桥接指令之后,我编写了下面的代码,以将此功能引入 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 应用程序一打开就会崩溃。我注意到它在实例化将上下文作为参数的 SwipeHandler 时崩溃: handler = new SwipeHandler(getReactApplicationContext()); .我犯了什么错误?该应用程序具有所有必要的权限。我想提一下,在创建完全原生的 android 版本时,应用程序的行为方式相同,直到我停止使用“即时运行”。

手机崩溃日志(华为 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 Intent 是“粘性的”。这意味着 HEADSET_PLUG Intent 可以在系统中徘徊,并在应用程序注册接收器后立即被应用程序接收。这可以解释为什么这些事件在 SwipeHander 仍在构建自身时出现时会产生错误。所以解决这个问题的一种可能方法(假设上面的分析是正确的)是在创建新的 SwipeHandler 之前读取现有的 Intent 。这是一个可能有效的示例(取自 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");
}
}
}
}

注意:我认为创建、注册和注销可能应该移到 Activity 的 onStart() 和 onStop() 生命周期方法中。 See here .

从 logcat 中,错误是由于在 SwipeHandler 类中接收到 NullPointerException 引起的。您尚未发布 SwipeHandler 类的代码,因此我无法进一步诊断。问题发生在这里:
com.imagpay.SwipeHandler.isReadable(SourceFile:655)

它试图从 isReadable 方法中的空对象计算状态。

可能是 imagpay 库被开发用于 Android 应用程序,而不是专门用于 React Native 应用程序。在这种情况下,问题可能源于您在每种情况下创建 SwipeHandler 的方式。上面代码中有两个地方初始化 SwipeHandler,其中之一是 React Native 版本:
handler = new SwipeHandler(getReactApplicationContext());

也许您可以尝试给它一个实际 Android 应用程序上下文的引用。就像是:
 handler = new SwipeHandler(getReactApplicationContext().getBaseContext());

关于java - 当我尝试从 Java 桥接设备监听模块时,为什么我的 Android 会立即响应 native 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42426279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com