【问题标题】:ArrayIndexOutOfBoundsException error when writing to a tag写入标签时出现 ArrayIndexOutOfBoundsException 错误
【发布时间】:2014-04-06 05:44:24
【问题描述】:

我遵循了一个关于如何将 NDEF 消息写入标签的教程。现在当我运行它时。它检测到标签,当我按下按钮写入消息时,应用程序崩溃。它给了我 JAVA.LANG.ArrayIndexOutOfBoundsException 有人可以帮我解决我做错了什么。

这是我在 logcat 中看到的错误。

感谢任何帮助。

代码如下:

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WriteMessage extends Activity {

    NfcAdapter adapter;
    PendingIntent pendingIntent;
    IntentFilter writeTagFilters[];
    boolean writeMode;
    Tag myTag;
    Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_message);
        ctx=this;

        Button WriteTag = (Button) findViewById (R.id.WriteTag);
        final TextView Message = (TextView) findViewById (R.id.MessageBox);

        WriteTag.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try{
                    if(myTag==null){
                        Toast.makeText(ctx, "Error_detected", Toast.LENGTH_LONG).show();
                    }else{
                        write(Message.getText().toString(), myTag);
                        Toast.makeText(ctx, "Ok_Writing", Toast.LENGTH_LONG).show();
                    }
                }catch(IOException e){
                    Toast.makeText(ctx, "Error_Writing", Toast.LENGTH_LONG).show();
                    e.printStackTrace();

                //} catch(FormatException e){

                    e.printStackTrace();
                } catch (FormatException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(ctx, "Error_Writing", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }



        }
        });

        adapter = NfcAdapter.getDefaultAdapter(this);
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent (this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter tagDetected = new IntentFilter (NfcAdapter.ACTION_TAG_DISCOVERED);
        tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
        writeTagFilters = new IntentFilter[] {tagDetected };

    }




    private void write(String text, Tag myTag)throws IOException, FormatException {

        NdefRecord[] records = {createRecord(text)};
        NdefMessage message = new NdefMessage(records);
        Ndef ndef = Ndef.get(myTag);
        ndef.connect();
        ndef.writeNdefMessage(message);
        ndef.close();
}

    private NdefRecord createRecord (String text ) throws UnsupportedEncodingException {

        String lang = "en";
        byte[] textBytes = text.getBytes();
        byte[] langBytes = lang.getBytes("US-ASCII");
        int langLength = langBytes.length;
        int textLength = textBytes.length;

        byte[] payload = new byte [1+ langLength + textLength ];
        payload[0] = (byte) langLength;

        System.arraycopy(langBytes, 0, payload, 1, langLength);
        System.arraycopy(langBytes, 0, payload, 1 + langLength, textLength);


        NdefRecord recordNFC = new NdefRecord (NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
        return recordNFC;
    }


    @Override
    protected void onNewIntent(Intent intent){
        if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
            myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);    
            Toast.makeText(this, "ok_detection" + myTag.toString(), Toast.LENGTH_LONG ).show();
        }
    }

    @Override
    public void onPause(){
        super.onPause();
        WriteModeOff();
    }

    @Override
    public void onResume(){
        super.onResume();
        WriteModeOn();
    }

    private void WriteModeOn(){
        writeMode = true;
        adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
    }

    private void WriteModeOff(){
        writeMode = false;
        adapter.disableForegroundDispatch(this);
    }



}

【问题讨论】:

    标签: arrays exception tags indexing nfc


    【解决方案1】:

    看起来createRecord()langBytes 复制到payload 两次,而不是复制textBytes,但长度为textBytes。如果textByteslangBytes 长,它将找不到足够的数据从源中复制。

    请参阅documentation on arraycopy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2015-05-22
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多