更改背景颜色崩溃应用

时间:2017-07-28 08:06:46

标签: java android

这个功能正在崩溃我的应用程序,我不知道为什么。

    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);
        }
    }

当我删除该功能并调用它时,该应用程序运行正常。 我是新手Android但有java经验。 在默认情况下在该方法中出现的所有内容之后,对onCreate方法的调用。 我没有在布局xml文件中放任何东西,除了onCreate在我的主要活动中我没有其他功能。

编辑1:删除计时器会使应用程序崩溃。如何将代码函数替换为使代码运行一段时间的东西?

编辑2: Logcat:---------崩溃的开始 07-28 15:15:37.259 32203-32203 /? E / AndroidRuntime:致命异常:主要                                                    过程:io.eliot.colorstop,PID:32203                                                    java.lang.RuntimeException:无法启动活动ComponentInfo {io.eliot.colorstop / io.eliot.colorstop.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android.view.View。对空引用引用的getRootView()'                                                        在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)                                                        at 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)                                                     引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View android.view.View.getRootView()'                                                        at io.eliot.colorstop.MainActivity.whileLoopF​​orChange(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)                                                        at 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)

在阅读logcat之后,似乎它获得了崩溃它的根视图。

1 个答案:

答案 0 :(得分:2)

Timer在新线程中运行代码。使用runOnUiThread函数

runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        //Your code here.
                    }
                });