java - Getting FATAL runtime error from LogCat -
this question has answer here:
unable figure out why error being produced @ runtime:
04-19 18:39:06.310 1741-1741/com.example.aaaaaj.studybuddy3 e/androidruntime: fatal exception: main process: com.example.aaaaaj.studybuddy3, pid: 1741 java.lang.runtimeexception: unable start activity componentinfo{com.example.uuj.benstudybuddy3/com.example.uuj.benstudybuddy3.lectureractivity}: java.lang.arrayindexoutofboundsexception: length=1; index=1 @ android.app.activitythread.performlaunchactivity(activitythread.java:2184) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2233) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method) caused by: java.lang.arrayindexoutofboundsexception: length=1; index=1 @ com.example.uuj.benstudybuddy3.lectureractivity.oncreate(lectureractivity.java:31) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2148) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2233) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method)
i have tried follow 'caused by' features still unable figure things out. below main activity - lectureractivity.
package com.example.aaaaaj.studybuddy3; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import java.util.arraylist; public class lectureractivity extends appcompatactivity { recyclerview recyclerview; recyclerview.adapter adapter; recyclerview.layoutmanager layoutmanager; arraylist<contact> list = new arraylist<contact>(); int[] image_id = {r.drawable.usericon}; string [] name, email, room; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_lecturer); name = getresources().getstringarray(r.array.lecturer_name); email = getresources().getstringarray(r.array.email_address); room = getresources().getstringarray(r.array.room); int count = 0; (string name : name) { contact contact = new contact(image_id[count],name,email[count],room[count]); count++; list.add(contact); } recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); recyclerview.sethasfixedsize(true); adapter = new contactadapter(list); recyclerview.setadapter(adapter); } }
i appreciate if give me point in right direction regarding issue - incredibly frustrating! , know simple.
<resources> <string name="app_name">studybuddy3</string> <string name="hello_world">hello world!</string> <string name="action_settings">settings</string> <string name="title_activity_lecturer">lectureractivity</string> <string-array name="lecturer_name"> <item>"dr. toby flenderson"</item> <item>"dr. michael scott"</item> <item>"dr. dwight schrute "</item> <item>"dr. jim halpert"</item> </string-array> <string-array name="email_address"> <item>"t.flenderson@dundermifflin.com"</item> <item>"m.scott@dundermifflin.com"</item> <item>"dk.schrute@dundermifflin.com"</item> <item>"j.halpert@dundermifflin.com"</item> </string-array> <string-array name="room"> <item>"16j14"</item> <item>"16j12"</item> <item>"16j15"</item> <item>"16j16"</item> </string-array>
the issue have 3 different arrays different number of elements running loop name
array length , causes arrayindexoutofboundsexception
other 2 arrays or 1 of other 2 arrays have less number of elements name
array.
name = getresources().getstringarray(r.array.lecturer_name); email = getresources().getstringarray(r.array.email_address); room = getresources().getstringarray(r.array.room);
all 3 declared arrays in xml should have same number of elements in order make code work . other wise have change logic.
now you've posted array xml, clear array lengths same. problem line -
contact contact = new contact(image_id[count],name,email[count],room[count]);
because in
int[] image_id = {r.drawable.usericon};
there 1 item in image_id
trying access wrong index. can change image_id
contain 4 elements other arrays or change in loop -
contact contact = new contact(image_id[0],name,email[count],room[count]);
Comments
Post a Comment