android - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo After tapping button to hit second screen activity -
trying make memory matching game grid of imagebuttons change image after tapping, can't second screen activity start. crashes , gives me logs posted below. attached manifest, , activity second screen.i know second screen activity worked when don't have buttons initialized globally , comment out functions. begginner please help!
android manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="eagle.abhishekravi.abhishek.eagle" > <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".secondscreen" android:label="operation" android:theme = "@style/apptheme"> </activity> </application> </manifest>
mainactivity
public void secondscreen(view view) { intent getnamescreenintent; getnamescreenintent = new intent(this, secondscreen.class); startactivity(getnamescreenintent); //finish(); }
secondscreen
package eagle.abhishekravi.abhishek.eagle; public class secondscreen extends activity implements view.onclicklistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout); intent activitythatcalled = getintent(); collections.shuffle(arrays.aslist(res)); iconrandomizer(); }; //initialize buttons in game imagebutton b1 = (imagebutton) findviewbyid(r.id.b1); imagebutton b2 = (imagebutton) findviewbyid(r.id.b2); imagebutton b3 = (imagebutton) findviewbyid(r.id.b3); imagebutton b4 = (imagebutton) findviewbyid(r.id.b4); imagebutton b5 = (imagebutton) findviewbyid(r.id.b5); imagebutton b6 = (imagebutton) findviewbyid(r.id.b6); imagebutton b7 = (imagebutton) findviewbyid(r.id.b7); imagebutton b8 = (imagebutton) findviewbyid(r.id.b8); imagebutton b9 = (imagebutton) findviewbyid(r.id.b9); imagebutton b10 = (imagebutton) findviewbyid(r.id.b10); imagebutton b11 = (imagebutton) findviewbyid(r.id.b11); imagebutton b12 = (imagebutton) findviewbyid(r.id.b12); imagebutton b13 = (imagebutton) findviewbyid(r.id.b13); imagebutton b14 = (imagebutton) findviewbyid(r.id.b14); imagebutton b15 = (imagebutton) findviewbyid(r.id.b15); imagebutton b16 = (imagebutton) findviewbyid(r.id.b16); imagebutton b17 = (imagebutton) findviewbyid(r.id.b17); imagebutton b18 = (imagebutton) findviewbyid(r.id.b18); imagebutton b19 = (imagebutton) findviewbyid(r.id.b19); imagebutton b20 = (imagebutton) findviewbyid(r.id.b20); //drawables int res[] = new int[] {r.drawable.brownbars,r.drawable.centeredorangedot, r.drawable.dots, r.drawable.greenlines, r.drawable.lightbulb, r.drawable.orangedots, r.drawable.orangelines, r.drawable.tree, r.drawable.yellow, r.drawable.yellowwithred}; public void iconrandomizer() { b1.setonclicklistener(this); b2.setonclicklistener(this); b3.setonclicklistener(this); b4.setonclicklistener(this); b5.setonclicklistener(this); b6.setonclicklistener(this); b7.setonclicklistener(this); } public void onclick(view v){ switch (v.getid()){ case r.id.b1: b1.setimageresource(res[0]); break; case r.id.b2: b2.setimageresource(res[1]); break; case r.id.b3: b3.setimageresource(res[2]); break; case r.id.b4: b4.setimageresource(res[3]); break; case r.id.b5: b5.setimageresource(res[4]); break; case r.id.b6: b6.setimageresource(res[5]); break; case r.id.b7: b7.setimageresource(res[6]); break; case r.id.b8: b8.setimageresource(res[7]); break; case r.id.b9: b9.setimageresource(res[8]); break; case r.id.b10: b10.setimageresource(res[9]); break; case r.id.b11: b11.setimageresource(res[0]); break; case r.id.b12: b12.setimageresource(res[1]); break; case r.id.b13: b13.setimageresource(res[2]); break; case r.id.b14: b14.setimageresource(res[3]); break; case r.id.b15: b15.setimageresource(res[4]); break; case r.id.b16: b16.setimageresource(res[5]); break; case r.id.b17: b17.setimageresource(res[6]); break; case r.id.b18: b18.setimageresource(res[7]); break; case r.id.b19: b19.setimageresource(res[8]); break; case r.id.b20: b20.setimageresource(res[9]); break; } } }
you have code following:
imagebutton b1 = (imagebutton) findviewbyid(r.id.b1);
the problem code execute during construction, @ time activity has no layout findviewbyid()
doesn't work. can declare members, need assign them after have called setcontentview()
.
imagebutton b1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout); // assign view references b1 = (imagebutton) findviewbyid(r.id.b1); ... }
Comments
Post a Comment