尝试在空对象引用NFC上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'

时间:2019-07-19 08:38:33

标签: android nfc

我在创建nfc阅读器应用程序时遇到此错误。当我以启动器活动是读取nfc标签的启动器运行该应用程序时,一切正常,但是当我使用另一个活动作为启动器活动时,单击按钮以打开nfc读取活动时,该应用程序因错误而崩溃。这是我的代码

private NfcAdapter mNfcAdapter;
private IntentFilter[] mTagFilters;
private PendingIntent mNfcPendingIntent;

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

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
            getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);

    IntentFilter ndefDiscovery = new IntentFilter(
            NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter tagDiscovery = new IntentFilter(
            NfcAdapter.ACTION_TAG_DISCOVERED);

    mTagFilters = new IntentFilter[]{ ndefDiscovery, tagDiscovery };

    readTag(getIntent());
}

@Override
protected void onResume() {
    super.onResume();
    if (mNfcAdapter != null) {
        mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent,
                mTagFilters, null);
    } else {
        Toast.makeText(this, "Sorry, No NFC Adapter found.",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (mNfcAdapter != null)
        mNfcAdapter.disableForegroundDispatch(this);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    readTag(intent);
}

private void readTag(Intent intent) {
    if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || intent.getAction()
            .equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {

        findViewById(R.id.progress).setVisibility(View.GONE);

        // Validate that this tag can be read
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        // Check to see if tag is writeable
        boolean writeable = writableTag(detectedTag);
        TextView writeableText = (TextView) findViewById(R.id.tag_writeable);
        writeableText.setText("Writeable? " + (writeable ? "Yes" : "No"));

        Ndef ndef = Ndef.get(detectedTag);
        if (ndef != null) {
            try {
                ndef.connect();

                int maxSize = ndef.getMaxSize();

                TextView infoText = (TextView) findViewById(R.id.tag_content);
                TextView maxSizeText = (TextView) findViewById(R.id.tag_max_size);
                maxSizeText.setText("Tag capacity: " + maxSize + " bytes");
                TextView remainingSizeText = (TextView) findViewById(R.id
                        .tag_remaining_size);

                NdefMessage message = ndef.getNdefMessage();
                if (message != null) {
                    NdefRecord[] records = message.getRecords();
                    StringBuilder sb = new StringBuilder();
                    for (NdefRecord record : records) {
                        String payload = new String(record.getPayload(), "UTF-8");
                        sb.append(payload + "\n");
                    }
                    String regNum = sb.toString().substring(3);

                    infoText.setText(regNum);
                    infoText.setTextColor(Color.BLACK);
                    //Intent intentProfile = new Intent(MainActivity.this, ProfileActivity.class);
                    //intentProfile.putExtra("id",regNum);
                    //startActivity(intentProfile);

                    remainingSizeText.setText("Remaining capacity: " + (maxSize - message
                            .getByteArrayLength()) + " bytes");
                } else {
                    infoText.setText("Empty tag");
                    infoText.setTextColor(Color.RED);

                    remainingSizeText.setText("Remaining capacity: " + maxSize + " bytes");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (FormatException e) {
                e.printStackTrace();
            } finally {
                try {
                    // Important if you want to use detectedTag later
                    ndef.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

private boolean writableTag(Tag tag) {

    try {
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            if (!ndef.isWritable()) {
                ndef.close();
                return false;
            }

            ndef.close();
            return true;
        }
    } catch (Exception e) {
        Toast.makeText(this, "Failed to read tag", Toast.LENGTH_SHORT)
                .show();
    }

    return false;
}

这是完整的日志

 Process: tino.varconn.nfccheckin, PID: 6047
java.lang.RuntimeException: Unable to start activity ComponentInfo{tino.varconn.nfccheckin/tino.varconn.nfccheckin.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3403)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3587)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2185)
    at android.os.Handler.dispatchMessage(Handler.java:112)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7593)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at tino.varconn.nfccheckin.MainActivity.readTag(MainActivity.java:78)
    at tino.varconn.nfccheckin.MainActivity.onCreate(MainActivity.java:49)
    at android.app.Activity.performCreate(Activity.java:7458)
    at android.app.Activity.performCreate(Activity.java:7448)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3382)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3587) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2185) 
    at android.os.Handler.dispatchMessage(Handler.java:112) 
    at android.os.Looper.loop(Looper.java:216) 
    at android.app.ActivityThread.main(ActivityThread.java:7593) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

2 个答案:

答案 0 :(得分:0)

您正在readTag(getIntent());中呼叫Oncreate(),而他的角色是getIntent,您还没有Intent。 ÿ 您必须使用setIntent(Intent intent)才能使用getIntent()

我没有确切要做什么? 如果要使用其他活动中的Intent,则应将其发送到其他活动中。

答案 1 :(得分:-1)

请评论readTag(getIntent())方法中的onCreate(,然后检查