java - Function to change background color crashing app -
this funcion crashing app , im not sure why.
public void whileloopforchange(){ while (gamerunning = true){ view someview = findviewbyid(r.id.screen); final view root = someview.getrootview(); random rand = new random(); final int value = rand.nextint(15); if(lvl<10){ new timer().schedule(new timertask() { @override public void run() { if(value == 1){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.red)); currentcolor = "red"; }else if(value == 2){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.pink)); currentcolor = "pink"; }else if(value == 3){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.orange)); currentcolor = "orange"; }else if(value == 4){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.blue)); currentcolor = "blue"; }else if(value == 5){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.green)); currentcolor = "green"; }else if(value == 6){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.white)); currentcolor = "white"; }else if(value == 7){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.black)); currentcolor = "black"; }else if(value == 8){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.salmon)); currentcolor = "salmon"; }else if(value == 9){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.violet)); currentcolor = "violet"; }else if(value == 10){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.cyan)); currentcolor = "cyan"; }else if(value == 11){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.darkgray)); currentcolor = "darkgray"; }else if(value == 12){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.gray)); currentcolor = "gray"; }else if(value == 13){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.chocolate)); currentcolor = "chocolate"; }else if(value == 14){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.silver)); currentcolor = "silver"; } } }, 1000); }else if(lvl<15 && lvl>9) { new timer().schedule(new timertask() { @override public void run() { if(value == 1){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.red)); currentcolor = "red"; }else if(value == 2){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.pink)); currentcolor = "pink"; }else if(value == 3){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.orange)); currentcolor = "orange"; }else if(value == 4){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.blue)); currentcolor = "blue"; }else if(value == 5){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.green)); currentcolor = "green"; }else if(value == 6){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.white)); currentcolor = "white"; }else if(value == 7){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.black)); currentcolor = "black"; }else if(value == 8){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.salmon)); currentcolor = "salmon"; }else if(value == 9){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.violet)); currentcolor = "violet"; }else if(value == 10){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.cyan)); currentcolor = "cyan"; }else if(value == 11){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.darkgray)); currentcolor = "darkgray"; }else if(value == 12){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.gray)); currentcolor = "gray"; }else if(value == 13){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.chocolate)); currentcolor = "chocolate"; }else if(value == 14){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.silver)); currentcolor = "silver"; } } }, 750); }else if(lvl<20 && lvl>14){ new timer().schedule(new timertask() { @override public void run() { if(value == 1){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.red)); currentcolor = "red"; }else if(value == 2){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.pink)); currentcolor = "pink"; }else if(value == 3){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.orange)); currentcolor = "orange"; }else if(value == 4){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.blue)); currentcolor = "blue"; }else if(value == 5){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.green)); currentcolor = "green"; }else if(value == 6){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.white)); currentcolor = "white"; }else if(value == 7){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.black)); currentcolor = "black"; }else if(value == 8){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.salmon)); currentcolor = "salmon"; }else if(value == 9){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.violet)); currentcolor = "violet"; }else if(value == 10){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.cyan)); currentcolor = "cyan"; }else if(value == 11){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.darkgray)); currentcolor = "darkgray"; }else if(value == 12){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.gray)); currentcolor = "gray"; }else if(value == 13){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.chocolate)); currentcolor = "chocolate"; }else if(value == 14){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.silver)); currentcolor = "silver"; } } }, 600); }else if(lvl<30 && lvl>19){ new timer().schedule(new timertask() { @override public void run() { if(value == 1){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.red)); currentcolor = "red"; }else if(value == 2){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.pink)); currentcolor = "pink"; }else if(value == 3){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.orange)); currentcolor = "orange"; }else if(value == 4){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.blue)); currentcolor = "blue"; }else if(value == 5){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.green)); currentcolor = "green"; }else if(value == 6){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.white)); currentcolor = "white"; }else if(value == 7){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.black)); currentcolor = "black"; }else if(value == 8){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.salmon)); currentcolor = "salmon"; }else if(value == 9){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.violet)); currentcolor = "violet"; }else if(value == 10){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.cyan)); currentcolor = "cyan"; }else if(value == 11){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.darkgray)); currentcolor = "darkgray"; }else if(value == 12){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.gray)); currentcolor = "gray"; }else if(value == 13){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.chocolate)); currentcolor = "chocolate"; }else if(value == 14){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.silver)); currentcolor = "silver"; } } }, 550); }else if(lvl<50 && lvl>29){ new timer().schedule(new timertask() { @override public void run() { if(value == 1){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.red)); currentcolor = "red"; }else if(value == 2){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.pink)); currentcolor = "pink"; }else if(value == 3){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.orange)); currentcolor = "orange"; }else if(value == 4){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.blue)); currentcolor = "blue"; }else if(value == 5){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.green)); currentcolor = "green"; }else if(value == 6){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.white)); currentcolor = "white"; }else if(value == 7){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.black)); currentcolor = "black"; }else if(value == 8){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.salmon)); currentcolor = "salmon"; }else if(value == 9){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.violet)); currentcolor = "violet"; }else if(value == 10){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.cyan)); currentcolor = "cyan"; }else if(value == 11){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.darkgray)); currentcolor = "darkgray"; }else if(value == 12){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.gray)); currentcolor = "gray"; }else if(value == 13){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.chocolate)); currentcolor = "chocolate"; }else if(value == 14){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.silver)); currentcolor = "silver"; } } }, 500); }else if(lvl<49){ new timer().schedule(new timertask() { @override public void run() { if(value == 1){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.red)); currentcolor = "red"; }else if(value == 2){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.pink)); currentcolor = "pink"; }else if(value == 3){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.orange)); currentcolor = "orange"; }else if(value == 4){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.blue)); currentcolor = "blue"; }else if(value == 5){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.green)); currentcolor = "green"; }else if(value == 6){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.white)); currentcolor = "white"; }else if(value == 7){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.black)); currentcolor = "black"; }else if(value == 8){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.salmon)); currentcolor = "salmon"; }else if(value == 9){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.violet)); currentcolor = "violet"; }else if(value == 10){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.cyan)); currentcolor = "cyan"; }else if(value == 11){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.darkgray)); currentcolor = "darkgray"; }else if(value == 12){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.gray)); currentcolor = "gray"; }else if(value == 13){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.chocolate)); currentcolor = "chocolate"; }else if(value == 14){ root.setbackgroundcolor(contextcompat.getcolor(mainactivity.this, r.color.silver)); currentcolor = "silver"; } } }, 350); } }
the app runs fine when remove function , call it. im pretty new android have java experiance. call function in oncreate method after defaultly comes in method. havent put in layout xml file , have no other functions oncreate in main activity.
edit 1: removing timer makes app not crash. how replace timer function makes code run after second?
edit 2: logcat: --------- beginning of crash 07-28 15:15:37.259 32203-32203/? e/androidruntime: fatal exception: main process: io.eliot.colorstop, pid: 32203 java.lang.runtimeexception: unable start activity componentinfo{io.eliot.colorstop/io.eliot.colorstop.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.view.getrootview()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2984) @ android.app.activitythread.handlelaunchactivity(activitythread.java:3045) @ android.app.activitythread.-wrap14(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1642) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6776) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1520) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1410) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.view.getrootview()' on null object reference @ io.eliot.colorstop.mainactivity.whileloopforchange(mainactivity.java:34) @ io.eliot.colorstop.mainactivity.oncreate(mainactivity.java:29) @ android.app.activity.performcreate(activity.java:6955) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1126) @ android.app.activitythread.performlaunchactivity(activitythread.java:2927) @ android.app.activitythread.handlelaunchactivity(activitythread.java:3045) @ android.app.activitythread.-wrap14(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1642) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6776) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1520) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1410)
after reading logcat seems getting root view crashes it.
timer runs code in new thread. use runonuithread function
runonuithread(new runnable() { @override public void run() { //your code here. } });
Comments
Post a Comment