尝试运行android应用程序交换消息时找不到错误

f8rj6qna  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(315)

我无法在代码中找到导致nullpointerexception的错误。该应用程序的设计目的是通过nfc接收输入并向另一个android设备发送一个字符串或uri,但该应用程序在启动时不断崩溃。
在应用程序启动时,它会显示“应用程序不断停止”。
它也发生在我的物理设备上。
日志如下:

2020-12-11 06:03:08.580 8013-8013/com.example.messageswap E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.messageswap, PID: 8013
    java.lang.RuntimeException: Unable to resume activity {com.example.messageswap/com.example.messageswap.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.messageswap.NFCManager.verifyNFC()' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4205)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
        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:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.messageswap.NFCManager.verifyNFC()' on a null object reference
        at com.example.messageswap.MainActivity.onResume(MainActivity.java:126)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)
        at android.app.Activity.performResume(Activity.java:7939)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237) 
        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:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

失败的方法调用设置如下:
NFCManager.java :

public class NFCManager {

    private Activity activity;
    private NfcAdapter nfcAdpt;

    public NFCManager(Activity activity) {
        this.activity = activity;
    }

    public void verifyNFC() throws NFCNotSupported, NFCNotEnabled {

        nfcAdpt = NfcAdapter.getDefaultAdapter(activity);

        if (nfcAdpt == null)
            throw new NFCNotSupported();

        if (!nfcAdpt.isEnabled())
            throw new NFCNotEnabled();

}

它被叫来了 MainActivity.java :

@Override
    protected void onResume() {
        // Register android app to receive notification when the device is near NFC tag using
        // foreground dispatch
        super.onResume();
        try {
            nfcMger.verifyNFC();
            Intent nfcIntent = new Intent(this, getClass());
            nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent =
                    PendingIntent.getActivity(this, 0, nfcIntent, 0);
            IntentFilter[] intentFiltersArray = new IntentFilter[]{};
            String[][] techList = new String[][]{
                    {android.nfc.tech.Ndef.class.getName()},
                    {android.nfc.tech.NdefFormatable.class.getName()}
            };

            NfcAdapter nfcAdpt = NfcAdapter.getDefaultAdapter(this);
            nfcAdpt.enableForegroundDispatch(this, pendingIntent,
                    intentFiltersArray, techList);
        } catch (NFCManager.NFCNotSupported nfcnsup) {
            Snackbar.make(v, "NFC not supported", Snackbar.LENGTH_LONG).show();
        } catch (NFCManager.NFCNotEnabled nfcnEn) {
            Snackbar.make(v, "NFC Not enabled", Snackbar.LENGTH_LONG).show();
        }

    }

我所能想到的就是这个设备抛出了一个异常,因为由于某种原因,它无法读取附近的任何nfc标签。
由于我对android开发不是很熟悉(只是最近才开始的),任何帮助诊断这个问题都将不胜感激。
先谢谢你。

swvgeqrz

swvgeqrz1#

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.messageswap.NFCManager.verifyNFC()' on a null object reference
    at com.example.messageswap.MainActivity.onResume(MainActivity.java:126)

似乎你的对象nfcmger初始化不正确。

相关问题