在线程中重击一个痣 - setButton

时间:2017-06-28 14:24:05

标签: java android multithreading

我做了一个类似于击打鼹鼠的游戏。而不是痣,你必须点击“x”。 我想随机改变“x”的位置,但我有以下错误“错误:找不到符号方法getButton1()” (zufall在德语中意为随机)

主题zufallsXThread = new Thread(){

        @Override
        public void run() {
            while (fail > 3) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int zufall = (int) (Math.random() * 9) + 1;

                        if (zufall == 1) {
                            this.getButton1().setText("x");
                        } else if (zufall == 2) {
                            this.getButton2().post("x");
                        } else if (zufall == 3) {
                            this.getButton3().setText("x");
                        } else if (zufall == 4) {
                            this.getButton4().setText("x");
                        } else if (zufall == 5) {
                            this.getButton5().setText("x");
                        } else if (zufall == 6) {
                            this.getButton6().setText("x");
                        } else if (zufall == 7) {
                            this.getButton7().setText("x");
                        } else if (zufall == 8) {
                            this.getButton8().setText("x");
                        } else if (zufall == 9) {
                            this.getButton9().setText("x");
                        }

                    }
                });

            }
        }
    };

1 个答案:

答案 0 :(得分:1)

从方法调用中删除this关键字。

if (zufall == 1) {
     getButton1().setText("x");
 }

并在run方法中更改所有内容。