android - How to disable all the buttons in the layout for a certain amount of time, java.NullPointerException -
secondscreen.java
public class secondscreen extends activity implements view.onclicklistener {
//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, 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}; int lay1, lay2, shufflecount = 0, gamecount = 0; imagebutton first, second; linearlayout layout; imagebutton b1; imagebutton b2; imagebutton b3; imagebutton b4; imagebutton b5; imagebutton b6; imagebutton b7; imagebutton b8; imagebutton b9; imagebutton b10; imagebutton b11; imagebutton b12; imagebutton b13; imagebutton b14; imagebutton b15; imagebutton b16; imagebutton b17; imagebutton b18; imagebutton b19; imagebutton b20; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout); intent activitythatcalled = getintent(); //initialize buttons in game b1 = (imagebutton) findviewbyid(r.id.b1); b2 = (imagebutton) findviewbyid(r.id.b2); b3 = (imagebutton) findviewbyid(r.id.b3); b4 = (imagebutton) findviewbyid(r.id.b4); b5 = (imagebutton) findviewbyid(r.id.b5); b6 = (imagebutton) findviewbyid(r.id.b6); b7 = (imagebutton) findviewbyid(r.id.b7); b8 = (imagebutton) findviewbyid(r.id.b8); b9 = (imagebutton) findviewbyid(r.id.b9); b10 = (imagebutton) findviewbyid(r.id.b10); b11 = (imagebutton) findviewbyid(r.id.b11); b12 = (imagebutton) findviewbyid(r.id.b12); b13 = (imagebutton) findviewbyid(r.id.b13); b14 = (imagebutton) findviewbyid(r.id.b14); b15 = (imagebutton) findviewbyid(r.id.b15); b16 = (imagebutton) findviewbyid(r.id.b16); b17 = (imagebutton) findviewbyid(r.id.b17); b18 = (imagebutton) findviewbyid(r.id.b18); b19 = (imagebutton) findviewbyid(r.id.b19); b20 = (imagebutton) findviewbyid(r.id.b20); iconrandomizer(); }; public void randomcheck(imagebutton btn, int image) { if (gamecount < 2) { gamecount++; if (gamecount == 1) { first = btn; lay1 = image; } else { second = btn; lay2 = image; if (lay1 != lay2) { arraylist<view> touchables = layout.gettouchables(); for(view touchable : touchables) { if (touchable instanceof button) ((button) touchable).setenabled(false); } handler handler = new handler(); handler.postdelayed(new runnable() { public void run() { first.setimageresource(r.color.material_blue_grey_800); second.setimageresource(r.color.material_blue_grey_800); first.setenabled(true); second.setenabled(true); } }, 1000); } gamecount = 0; } } } static void shufflearray(int[] ar) { random rnd = new random(); (int = ar.length - 1; > 0; i--) { int index = rnd.nextint(i + 1); // simple swap int = ar[index]; ar[index] = ar[i]; ar[i] = a; } } public void iconrandomizer() { (int = 0; < 10; i++) system.out.println(res[i]); shufflearray(res); (int = 0; < 10; i++) system.out.println(res[i]); b1.setonclicklistener(this); b2.setonclicklistener(this); b3.setonclicklistener(this); b4.setonclicklistener(this); b5.setonclicklistener(this); b6.setonclicklistener(this); b7.setonclicklistener(this); b8.setonclicklistener(this); b9.setonclicklistener(this); b10.setonclicklistener(this); b11.setonclicklistener(this); b12.setonclicklistener(this); b13.setonclicklistener(this); b14.setonclicklistener(this); b15.setonclicklistener(this); b16.setonclicklistener(this); b17.setonclicklistener(this); b18.setonclicklistener(this); b19.setonclicklistener(this); b20.setonclicklistener(this); } public void onclick(view v){ switch (v.getid()){ case r.id.b1: b1.setimageresource(res[0]); b1.setenabled(false); randomcheck(b1, res[0]); break; case r.id.b2: b2.setimageresource(res[1]); b2.setenabled(false); randomcheck(b2,res[1]); break; case r.id.b3: b3.setimageresource(res[2]); b3.setenabled(false); randomcheck(b3, res[2]); break; case r.id.b4: b4.setimageresource(res[3]); b4.setenabled(false); randomcheck(b4, res[3]); break; case r.id.b5: b5.setimageresource(res[4]); b5.setenabled(false); randomcheck(b5, res[4]); break; case r.id.b6: b6.setimageresource(res[5]); b6.setenabled(false); randomcheck(b6, res[5]); break; case r.id.b7: b7.setimageresource(res[6]); b7.setenabled(false); randomcheck(b7, res[6]); break; case r.id.b8: b8.setimageresource(res[7]); b8.setenabled(false); randomcheck(b8, res[7]); break; case r.id.b9: b9.setimageresource(res[8]); b9.setenabled(false); randomcheck(b9, res[8]); break; case r.id.b10: b10.setimageresource(res[9]); b10.setenabled(false); randomcheck(b10, res[9]); break; case r.id.b11: b11.setimageresource(res[10]); b11.setenabled(false); randomcheck(b11, res[10]); break; case r.id.b12: b12.setimageresource(res[11]); b12.setenabled(false); randomcheck(b12, res[11]); case r.id.b13: b13.setimageresource(res[12]); b13.setenabled(false); randomcheck(b13, res[12]); break; case r.id.b14: b14.setimageresource(res[13]); b14.setenabled(false); randomcheck(b14, res[13]); case r.id.b15: b15.setimageresource(res[14]); b15.setenabled(false); randomcheck(b15, res[14]); break; case r.id.b16: b16.setimageresource(res[15]); b16.setenabled(false); randomcheck(b16, res[15]); break; case r.id.b17: b17.setimageresource(res[16]); b17.setenabled(false); randomcheck(b17, res[16]); break; case r.id.b18: b18.setimageresource(res[17]); b18.setenabled(false); randomcheck(b18, res[17]); break; case r.id.b19: b19.setimageresource(res[18]); b19.setenabled(false); randomcheck(b19, res[18]); break; case r.id.b20: b20.setimageresource(res[19]); b20.setenabled(false); randomcheck(b20, res[19]); break; } }
//}
}
error log:
java.lang.nullpointerexception: attempt invoke virtual method 'java.util.arraylist android.widget.linearlayout.gettouchables()' on null object reference @ eagle.abhishekravi.abhishek.eagle.secondscreen.randomcheck(secondscreen.java:111) @ eagle.abhishekravi.abhishek.eagle.secondscreen.onclick(secondscreen.java:232) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
i making memory match game, , need disable buttons after second button
tapped. after check complete, resume normally. have got checks work still need buttons disabled in case user taps button
between 1 second pause.let me know if need more information. thanks!!
based on information provided error message, looks layout
null. if you're still having trouble, code layout
being set helpful.
your layout should accessed via r.layout.my_layout
.
Comments
Post a Comment