-2

When I compile/build my app, it creates my APK without any errors. Additionally, in Android Studio, I don't have error notifications. So I expect the app to work. However, when I install and open the app, as soon as I scan an NFC tag, I get the error message "Unfortunately BMT_Admin has stopped working".

The only thing I'm trying to do is write an external record to the tag, called "payload", and then also write an AAR (Android Application Record) to the tag that can be called by future scans. The code that I'm using follows:

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

    if(intent.hasExtra(NfcAdapter.EXTRA_TAG))
    {
        Toast.makeText(this, "NFC Scan", Toast.LENGTH_SHORT).show();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        byte[] payload = "my string_tag1".getBytes();

        NdefRecord[] ndefRecords = new NdefRecord[0];
        ndefRecords[0] = NdefRecord.createExternal("nfctutorials", "externaltype", payload);
        ndefRecords[1] = NdefRecord.createApplicationRecord("com.example.myapp");
        NdefMessage ndefMessage = new NdefMessage(ndefRecords);
        writeNdefMessage(tag, ndefMessage);
    }
}

I'm assuming there is something I'm doing here that isn't correct and is throwing an error when I try to scan a tag. But I have no idea what that could be.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Leo
  • 21
  • 3
  • 4
    "I expect the app to work" -- there are **many** sorts of bugs that cannot be detected at compile time. "I get the error message "Unfortunately BMT_Admin has stopped working". " -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Sep 21 '16 at 17:52

1 Answers1

0

If you expect real help (other than wild guesses), you need to reveal the error message (i.e. the stack trace from ADB log) and the relevant code parts.

Besides that, from the code you have in your question, the most likely cause may be the line

NdefRecord[] ndefRecords = new NdefRecord[0];

There, you create an empty array. However, on the next two lines you try to access the non-existent indices 0 and 1:

ndefRecords[0] = NdefRecord.createExternal("nfctutorials", "externaltype", payload);
ndefRecords[1] = NdefRecord.createApplicationRecord("com.example.myapp");

This will obviously lead to an ArrayIndexOutOfBoundsexception since 0 and 1 are beyond the end of the empty array.

Consequently, you need to change the array allocation to

NdefRecord[] ndefRecords = new NdefRecord[2];

in order to allocate space for the two array elements.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Thanks Michael...I'm very, very new to this, so I appreciate the feedback and the suggestions!!! I had completely overlooked the 0 in my array config...it always helps to have another set of eyes take a look. I'll try this and go thru the stack trace before posting again. – Leo Sep 21 '16 at 18:11