React Native:Android 应用程序崩溃,onNewIntent
Posted
技术标签:
【中文标题】React Native:Android 应用程序崩溃,onNewIntent【英文标题】:React Native: Android app crashes, onNewIntent 【发布时间】:2017-05-09 08:49:45 【问题描述】:下面的 code-sn-p 是 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
我做错了什么?提前致谢。
【问题讨论】:
【参考方案1】:通过添加检查标记是否为空来修复:
@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);
【讨论】:
以上是关于React Native:Android 应用程序崩溃,onNewIntent的主要内容,如果未能解决你的问题,请参考以下文章
react-native-maps@0.29.3: Android - 空地图
react-native-pdf iOS 和 Android 构建错误
React-native run-android 构建了我的应用程序的正确版本,但 assembleRelease/react-native run-android --variant=release
使用 react-native 克隆本机 android 警报屏幕
使用 lottie-react-native (2.6.1) 时,React native expo 应用程序在 android 上崩溃,但适用于 ios