NFC enableForegroundDispatch崩溃的应用程序

时间:2014-09-12 03:13:27

标签: java android eclipse nfc android-applicationrecord

我试图让我的应用程序通过NFC AAR启动,但之后忽略所有NFC意图(我不希望我的应用程序在其已经运行时启动)。在这里筛选之后,我发现最好的方法是在我的活动中启用foregroundDispatch。我试图尽可能地遵循其他语法,但现在我的应用程序崩溃了("不幸的是,MyApp已停止")。任何帮助将非常感谢

的Manifest.xml:

    <application
    <activity
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="application/com.MyApp.frontcam" />
        </intent-filter>
    </activity>
    </application>

Activity.Java:

    private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;

@Override
public void onCreate(Bundle savedInstanceState) {

    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");  
    }
    catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {ndef, };

}

@Override
protected void onResume() {
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, null);
}

logcat的:

09-11 22:28:31.205: E/CameraPreview(2047): 176/144
09-11 22:29:04.756: D/AndroidRuntime(2047): Shutting down VM
09-11 22:29:04.756: W/dalvikvm(2047): threadid=1: thread exiting with uncaught exception (group=0xb0d0cb20)
09-11 22:29:04.826: E/AndroidRuntime(2047): FATAL EXCEPTION: main
09-11 22:29:04.826: E/AndroidRuntime(2047): Process: com.MyApp.frontcam, PID: 2047
09-11 22:29:04.826: E/AndroidRuntime(2047): java.lang.RuntimeException: Unable to resume activity {com.MyApp.frontcam/com.MyApp.frontcam.MainActivity}: java.lang.NullPointerException
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.os.Looper.loop(Looper.java:136)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at java.lang.reflect.Method.invoke(Method.java:515)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at dalvik.system.NativeStart.main(Native Method)
09-11 22:29:04.826: E/AndroidRuntime(2047): Caused by: java.lang.NullPointerException
09-11 22:29:04.826: E/AndroidRuntime(2047):     at com.MyApp.frontcam.MainActivity.onResume(MainActivity.java:106)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.Activity.performResume(Activity.java:5310)
09-11 22:29:04.826: E/AndroidRuntime(2047):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
09-11 22:29:04.826: E/AndroidRuntime(2047):     ... 12 more

1 个答案:

答案 0 :(得分:2)

mAdapter函数中的onResume似乎为空。

确保在使用变量之前对其进行初始化。

查看the documentation,首先需要获取NfcManager,然后从那里获取NfcAdapter个实例。

您可以为适配器创建一个getter方法,这样只有在尚未完成的情况下才设置该值。然后在onResume中,您可以调用该方法来获取适配器。

private NfcAdapter getAdapter() {
   if (mAdapter == null) {
       NfcManager manager = (NfcManager) getSystemServive(NFC_SERVICE);
       mAdapter = manager.getDefaultAdapter(this);
   }
   return mAdapter;
}

@Override
protected void onResume() {
    super.onResume();
    getAdapter().enableForegroundDispatch(this, mPendingIntent, mFilters, null);
}