gpt4 book ai didi

javascript - react native : Android app crashes, onNewIntent

转载 作者:太空狗 更新时间:2023-10-29 13:52:50 29 4
gpt4 key购买 nike

以下代码 fragment 是用于 react-native 的 NFC 模块。我用它读取了 NFC 标签 ID。问题是,如果您将应用程序置于后台,如果您再次单击应用程序图标,应用程序将崩溃,但如果您从任务列表中打开它,它会正常运行。

NFC 读取模块:

//package com.toastexample;
package net.opencampus.android.client;
import android.widget.Toast;
import android.util.Log;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.LifecycleEventListener;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import java.nio.ByteBuffer;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import android.support.annotation.Nullable;

import java.util.Map;
import java.util.HashMap;

public class NfcReadModule extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {
public static final String TAG = "NFCActivity";
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";

private ReactApplicationContext reactContext;

private static final char[] HAX_ARRAY = "0123456789ABCDEF".toCharArray();
String serialNumber;
String intentText = "Empty";
Promise tagPromise;

public NfcReadModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
this.reactContext.addActivityEventListener(this);
}

@Override
public String getName() {
return "NfcReadModule";
}

@Override
public void onNewIntent(Intent intent) {
intentText = "onNewIntent";
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
serialNumber = bytesToHex(tag.getId());
WritableMap params = Arguments.createMap();
params.putString("serial", serialNumber);
sendEvent(this.reactContext, "tagDetected", params);

}

@ReactMethod
public void getCardId(Promise promise) {
this.tagPromise = promise;
}

@Override
public void onHostResume() {

}

@Override
public void onHostPause() {

}

@Override
public void onHostDestroy() {

}

@Override
public void onActivityResult(
final Activity activity,
final int requestCode,
final int resultCode,
final Intent intent) {

}

@ReactMethod
public void show() {

}

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
return constants;
}

private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[bytes.length - j - 1] & 0xFF;
hexChars[j * 2] = HAX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HAX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
if (reactContext.hasActiveCatalystInstance()) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}
}

崩溃时来自 logcat 的日志:

05-09 10:34:03.457 22282 22282 E AndroidRuntime: FATAL EXCEPTION: main
05-09 10:34:03.457 22282 22282 E AndroidRuntime: Process: net.xxxx.android.client, PID: 22282
05-09 10:34:03.457 22282 22282 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] android.nfc.Tag.getId()' on a null object reference
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at net.xxxx.android.client.NfcReadModule.onNewIntent(NfcReadModule.java:61)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at com.facebook.react.bridge.ReactContext.onNewIntent(ReactContext.java:208)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at com.facebook.react.ReactInstanceManager.onNewIntent(ReactInstanceManager.java:503)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at com.facebook.react.ReactActivityDelegate.onNewIntent(ReactActivityDelegate.java:189)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at com.facebook.react.ReactActivity.onNewIntent(ReactActivity.java:99)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1223)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1235)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2802)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.ActivityThread.performNewIntents(ActivityThread.java:2817)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2833)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.ActivityThread.-wrap15(ActivityThread.java)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1556)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6119)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
05-09 10:34:03.457 22282 22282 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-09 10:34:03.467 607 2381 W ActivityManager: Force finishing activity net.xxxx.android.client/.MainActivity

我做错了什么?提前致谢。

最佳答案

通过添加检查标签是否不为空来修复:

@Override
public void onNewIntent(Intent intent) {
intentText = "onNewIntent";
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
serialNumber = bytesToHex(tag.getId());
WritableMap params = Arguments.createMap();
params.putString("serial", serialNumber);
sendEvent(this.reactContext, "tagDetected", params);
}
}

关于javascript - react native : Android app crashes, onNewIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43865226/

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