检查nfc标签是否在附近

问题描述 投票:0回答:1

我想让我的手机检测附近(靠近它的表面)是否有nfc标签。以下代码没有错误,但是一旦我运行该应用程序,它就会崩溃。如果你们中的某人可以浏览我的代码并检查是否有我看不到的东西,那将非常有帮助。以下是运行时错误。

public class AccessControlActivity extends AppCompatActivity {

    NfcAdapter nfcAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_access_control);

         nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        // Checks if there is NFC function
        if(nfcAdapter != null && nfcAdapter.isEnabled()) {
            //Toast.makeText(this, "NFC works", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "NFC is not available!", Toast.LENGTH_SHORT).show();
            //finish();
        }


    }


    @Override
    protected  void onNewIntent(Intent intent) {
        Toast.makeText(this, "NFC intent received", Toast.LENGTH_LONG).show();
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        Intent intent = new Intent(this, AccessControlActivity.class);
        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        IntentFilter[] intentFilters = new IntentFilter[]{};

        nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);


        super.onResume();
    }

    @Override
    protected void onPause() {
        nfcAdapter.disableForegroundDispatch(this);


        super.onPause();
    }
}

运行时错误看起来像这样:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nfc.netvision, PID: 5484
    java.lang.RuntimeException: Unable to resume activity {com.nfc.netvision/com.nfc.netvision.AccessControlActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4341)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4373)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7464)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
        at com.nfc.netvision.AccessControlActivity.onResume(AccessControlActivity.java:116)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)
        at android.app.Activity.performResume(Activity.java:8125)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4331)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4373) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7464) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955) 
I/Process: Sending signal. PID: 5484 SIG: 9
java android nfc
1个回答
0
投票

因此,您检查nfcAdapter != null中的onCreate并只是举杯,然后您的应用将盲目地尝试在null中使用可能的onResume适配器。

[这将解释Attempt to invoke virtual method中的on a null object reference onResume,因为nfcAdapter可能是null中的onResume

您应检查again中的onResume

同样,您的Intent过滤器看起来也不正确,它们的代码要么导致没有将Intent发送给您,要么相反,导致包括非NFC Intent在内的所有消息均发送给您。

出现任何类型的标签时都会调用更多的普通代码。

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndefDetected.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {}
        IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        IntentFilter[] nfcIntentFilter = new IntentFilter[]{ndefDetected,techDetected,tagDetected};

        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        if(nfcAdapter!= null)
            nfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if(nfcAdapter!= null)
            nfcAdapter.disableForegroundDispatch(this);
    }
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.