2014-03-04 108 views
0

我跟隨了關於如何將NDEF消息寫入標籤的教程。現在當我運行它。它檢測到標籤,並且當我按下按鈕來編寫消息和應用程序崩潰。它給了我JAVA.LANG.ArrayIndexOutOfBoundsException可以有人請幫我對我做錯了什麼。寫入標籤時發生ArrayIndexOutOfBoundsException錯誤

這是我在logcat中看到的錯誤。 enter image description here

任何幫助表示讚賞。

下面是代碼:

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); 
    } 



} 

回答

1

看起來createRecord()複印langBytes成​​而不是兩次複製textBytes,但隨着textBytes長度。如果textBytes長於langBytes,則它將找不到要從源複製的足夠數據。

查看documentation on arraycopy