java - ArrayIndexOutOfBoundsException After Scanning NFC Tag -


i'm attempting rebuild nfc tag reader

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

and i'm getting following error:

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

on line:

 ndefmessage msg2 = (ndefmessage) rawmsgs[1]; 

i understand issue related trying access array item doesn't exist i'm not sure how resolve issue in following implementation.

p.s.

the issue occurs when scanning new nfc tag. noticed interesting though...

if comment out following 2 lines app not crash , scans tag successfully!

// ndefmessage msg2 = (ndefmessage) rawmsgs[1];    //  beammsg2.settext(new string(msg.getrecords()[1].getpayload())); 

source:

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 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 not available on device.");             beammsg2.settext("nfc not available on device.");         } else {             // register callback set ndef message             mnfcadapter.setndefpushmessagecallback(this, this);             // register callback listen 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 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 onndefpushcompletecallback interface      */     @override     public void onndefpushcomplete(nfcevent arg0) {         // handler needed send messages activity when         // callback occurs, because happens binder thread         mhandler.obtainmessage(message_sent).sendtotarget();     }      /** handler receives message 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 see activity started due android beam         if (nfcadapter.action_ndef_discovered.equals(getintent().getaction())) {             processintent(getintent());         }     }      @override     public void onnewintent(intent intent) {         // onresume gets called after handle intent         setintent(intent);     }      /**      * parses ndef message intent , prints textview      */     void processintent(intent intent) {         parcelable[] rawmsgs = intent.getparcelablearrayextra(                 nfcadapter.extra_ndef_messages);         // 1 message sent during beam         ndefmessage msg = (ndefmessage) rawmsgs[0];         ndefmessage msg2 = (ndefmessage) rawmsgs[1];         // record 0 contains mime type, record 1 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 not available, won't needing 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);        // }     } //}//} 

edit after answer:

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 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 not available on device.");             beammsg2.settext("nfc not available on device.");         } else {             // register callback set ndef message             mnfcadapter.setndefpushmessagecallback(this, this);             // register callback listen 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 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 onndefpushcompletecallback interface      */     @override     public void onndefpushcomplete(nfcevent arg0) {         // handler needed send messages activity when         // callback occurs, because happens binder thread         mhandler.obtainmessage(message_sent).sendtotarget();     }      /** handler receives message 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 see activity started due android beam         if (nfcadapter.action_ndef_discovered.equals(getintent().getaction())) {             processintent(getintent());         }     }      @override     public void onnewintent(intent intent) {         // onresume gets called after handle intent         setintent(intent);     }      /**      * parses ndef message intent , prints textview      */     void processintent(intent intent) {         parcelable[] rawmsgs = intent.getparcelablearrayextra(                 nfcadapter.extra_ndef_messages);     // 1 message sent during beam     ndefmessage[] msg =  new ndefmessage[rawmsgs.length];     (int = 0; < msg.length; i++) {         msg[i] = (ndefmessage) rawmsgs[i];         // record 0 contains mime type, record 1 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 not available, won't needing 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);        // }     } //}//} 

your error intent you're sending on line:

parcelable[] rawmsgs = intent.getparcelablearrayextra(nfcadapter.extra_ndef_messages); 

where code use create intent?

loop edit:

to add loop more this:

parcelable[] rawmsgs = intent.getparcelablearrayextra(             nfcadapter.extra_ndef_messages); // 1 message sent during beam ndefmessage[] msg = = new ndefmessage[rawmsgs.length]; (int = 0; < msg.length; i++) {     msg[i] = (ndefmessage) rawmsgs[i];     // record 0 contains mime type, record 1 aar, if present     beammsg[i].settext(new string(msg[i].getrecords()[0].getpayload());     beammsg2[i].settext(new string(msg[i].getrecords()[1].getpayload())); } 

you have initialise beammsg , beammsg2 array of textviews too. or of course append text field , leave is.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -