2014-03-04 3 views
0

태그에 NDEF 메시지를 쓰는 방법에 대한 자습서를 읽었습니다. 그리고 지금 내가 그것을 실행할 때. 태그를 감지하고 버튼을 눌러 메시지를 작성하면 앱이 다운됩니다. 그것은 나를 JAVA.LANG.ArrayIndexOutOfBoundsException 누군가 날 잘못 도와 줄 수 있습니다.태그에 쓸 때 ArrayIndexOutOfBoundsException 오류가 발생했습니다.

이것은 logcat에서 보이는 오류입니다. enter image description here

어떤 도움을 주시면 감사하겠습니다. createRecord()는하지만 textBytes의 길이, payloadlangBytes 복사를 두 번 대신 textBytes을 복사하는 것처럼

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

가 보이는 : 여기

는 코드입니다. textByteslangBytes보다 길면 원본에서 복사 할 데이터가 충분하지 않습니다.

관련 문제