0

NFC タグ リーダーを再構築しようとしています

https://stackoverflow.com/questions/16071121/converting-existing-nfc-script-to-read-multiple-ndef-records

次のエラーが表示されます。

 java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

行で:

 NdefMessage msg2 = (NdefMessage) rawMsgs[1];

この問題は通常、存在しない配列項目にアクセスしようとすることに関連していることを理解していますが、次の実装で問題を解決する方法が本当にわかりません。

PS

この問題は、新しい NFC タグをスキャンするときにのみ発生します。非常に興味深いことに気付きましたが...

次の 2 行をコメント アウトすると、アプリはクラッシュせず、タグを正常にスキャンします。

// NdefMessage msg2 = (NdefMessage) rawMsgs[1];


 //  beamMsg2.setText(new String(msg.getRecords()[1].getPayload()));

ソース:

public class Nfc extends Activity implements CreateNdefMessageCallback,
        OnNdefPushCompleteCallback {
    NfcAdapter mNfcAdapter;
    TextView beamMsg;
    TextView beamMsg2;
    private static final int MESSAGE_SENT = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nfc);
        beamMsg = (TextView) findViewById(R.id.msg);
        beamMsg2 = (TextView) findViewById(R.id.msg_ID);
        beamMsg.setText("Peter Smith");
        beamMsg2.setText("123456");

        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            beamMsg = (TextView) findViewById(R.id.msg);
            beamMsg2= (TextView) findViewById(R.id.msg_ID);

            beamMsg.setText("NFC is not available on this device.");
            beamMsg2.setText("NFC is not available on this device.");
        } else {
            // Register callback to set NDEF message
            mNfcAdapter.setNdefPushMessageCallback(this, this);
            // Register callback to listen for message-sent success
            mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
        }
    }

    public void submitClicked(View v)
    {
        Toast toast = Toast.makeText(getApplicationContext(), R.string.info,
                Toast.LENGTH_LONG);
        toast.show();
    }
    /**
     * Implementation for the CreateNdefMessageCallback interface
     */
    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        NdefMessage msg = new NdefMessage(NdefRecord.createMime(
                beamMsg.getText()  .toString(), null));

        return msg;

    }

    public NdefMessage createNdefMessage_two(NfcEvent event) {
        NdefMessage msg2 = new NdefMessage(NdefRecord.createMime(
                beamMsg2.getText()  .toString(), null));

        return msg2;

    }




    /**
     * Implementation for the OnNdefPushCompleteCallback interface
     */
    @Override
    public void onNdefPushComplete(NfcEvent arg0) {
        // A handler is needed to send messages to the activity when this
        // callback occurs, because it happens from a binder thread
        mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
    }

    /** This handler receives a message from onNdefPushComplete */
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_SENT:
                Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
                break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        NdefMessage msg2 = (NdefMessage) rawMsgs[1];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        beamMsg.setText(new String(msg.getRecords()[0].getPayload()));
        beamMsg2.setText(new String(msg.getRecords()[1].getPayload()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // If NFC is not available, we won't be needing this menu
        if (mNfcAdapter == null) {
            return super.onCreateOptionsMenu(menu);
        }
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

   // @Override
   // public boolean onOptionsItemSelected(MenuItem item) {
    //    switch (item.getItemId()) {
      //      case R.id.menu_settings:
        //        Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
          //      startActivity(intent);
            //    return true;
         //   default:
           //     return super.onOptionsItemSelected(item);
       // }
    }
//}//}

回答後に編集:

public class Connect extends Activity implements CreateNdefMessageCallback,
        OnNdefPushCompleteCallback {
    NfcAdapter mNfcAdapter;
    TextView beamMsg;
    TextView beamMsg2;
    private static final int MESSAGE_SENT = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect);
        beamMsg = (TextView) findViewById(R.id.msg);
        beamMsg2 = (TextView) findViewById(R.id.msg_ID);
        beamMsg.setText("Peter Smith");
        beamMsg2.setText("123456");

        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            beamMsg = (TextView) findViewById(R.id.msg);
            beamMsg2= (TextView) findViewById(R.id.msg_ID);

            beamMsg.setText("NFC is not available on this device.");
            beamMsg2.setText("NFC is not available on this device.");
        } else {
            // Register callback to set NDEF message
            mNfcAdapter.setNdefPushMessageCallback(this, this);
            // Register callback to listen for message-sent success
            mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
        }
    }

    public void submitClicked(View v)
    {
        Toast toast = Toast.makeText(getApplicationContext(), R.string.info,
                Toast.LENGTH_LONG);
        toast.show();
    }
    /**
     * Implementation for the CreateNdefMessageCallback interface
     */
    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        NdefMessage msg = new NdefMessage(NdefRecord.createMime(
                beamMsg.getText()  .toString(), null));

        return msg;

    }

    public NdefMessage createNdefMessage_two(NfcEvent event) {
        NdefMessage msg2 = new NdefMessage(NdefRecord.createMime(
                beamMsg2.getText()  .toString(), null));

        return msg2;

    }




    /**
     * Implementation for the OnNdefPushCompleteCallback interface
     */
    @Override
    public void onNdefPushComplete(NfcEvent arg0) {
        // A handler is needed to send messages to the activity when this
        // callback occurs, because it happens from a binder thread
        mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
    }

    /** This handler receives a message from onNdefPushComplete */
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_SENT:
                Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
                break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage[] msg =  new NdefMessage[rawMsgs.length];
    for (int i = 0; i < msg.length; i++) {
        msg[i] = (NdefMessage) rawMsgs[i];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        beamMsg[i].setText(new String(msg[i].getRecords()[1].getPayload()));
        beamMsg2[i].setText(new String(msg[i].getRecords()[1].getPayload()));
    }}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // If NFC is not available, we won't be needing this menu
        if (mNfcAdapter == null) {
            return super.onCreateOptionsMenu(menu);
        }
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

   // @Override
   // public boolean onOptionsItemSelected(MenuItem item) {
    //    switch (item.getItemId()) {
      //      case R.id.menu_settings:
        //        Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
          //      startActivity(intent);
            //    return true;
         //   default:
           //     return super.onOptionsItemSelected(item);
       // }
    }
//}//}
4

2 に答える 2

0

実際、私は Java を扱っていませんが、やってみます。お聞きしたいのですが、getParcelableArrayExtra() の実装を見ることができます。なぜ彼は 2 つではなく 1 つのメッセージを返すのでしょうか?

于 2013-04-18T00:14:59.103 に答える
0

エラーはおそらく、次の行で送信している意図に関係しています。

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

インテントを作成するために使用するコードはどこにありますか?

ループ編集:

次のようにループを追加するには:

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage[] msg = = new NdefMessage[rawMsgs.length];
for (int i = 0; i < msg.length; i++) {
    msg[i] = (NdefMessage) rawMsgs[i];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    beamMsg[i].setText(new String(msg[i].getRecords()[0].getPayload());
    beamMsg2[i].setText(new String(msg[i].getRecords()[1].getPayload()));
}

beamMsg と beamMsg2 も TextView の配列として初期化する必要があります。またはもちろん、テキスト フィールドに追加してそのままにしておきます。

于 2013-04-18T00:15:47.363 に答える