Android - 线程问题(按钮中的随机字符切换,直到单击按钮)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android - 线程问题(按钮中的随机字符切换,直到单击按钮)相关的知识,希望对你有一定的参考价值。

我想在按钮中切换随机字符,直到按下停止按钮。当你按下停止按钮时,随机字符将显示为按钮文字。我设法做到的唯一方法是使用Threads。但我有问题,3-4次工作,但当我打开活动第三次或某些第四次应用程序崩溃。我不知道为什么会这样。我需要帮助来解决这个问题。如果有人知道如何在没有线程的情况下做到这一点,并希望帮助我会很感激。

这是app的图片:CLICK

public class NajduzaRec extends AppCompatActivity {

    private int brojac = 0;
    String randomLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //char[] randomLetters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'Z', 'Ž', 'Č', 'Ć', 'Đ', 'Š'};
    private boolean threadRunning=true;
    Random r = new Random();

    private static ManualResetEvent mre = new ManualResetEvent(false);

    EditText unosReciTxt;
    Button stopBtn, slovo1Btn,slovo2Btn,slovo3Btn,slovo4Btn,slovo5Btn,slovo6Btn,slovo7Btn,slovo8Btn,slovo9Btn,slovo10Btn,slovo11Btn,slovo12Btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_najduza_rec);

        stopBtn = (Button)findViewById(R.id.stopBtn);
        slovo1Btn = (Button)findViewById(R.id.slovo1Btn);
        slovo2Btn = (Button)findViewById(R.id.slovo2Btn);
        slovo3Btn = (Button)findViewById(R.id.slovo3Btn);
        slovo4Btn = (Button)findViewById(R.id.slovo4Btn);
        slovo5Btn = (Button)findViewById(R.id.slovo5Btn);
        slovo6Btn = (Button)findViewById(R.id.slovo6Btn);
        slovo7Btn = (Button)findViewById(R.id.slovo7Btn);
        slovo8Btn = (Button)findViewById(R.id.slovo8Btn);
        slovo9Btn = (Button)findViewById(R.id.slovo9Btn);
        slovo10Btn = (Button)findViewById(R.id.slovo10Btn);
        slovo11Btn = (Button)findViewById(R.id.slovo11Btn);
        slovo12Btn = (Button)findViewById(R.id.slovo12Btn);

        unosReciTxt = (EditText)findViewById(R.id.unosReciTxt);
        /*while(brojac==0) {
            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
            slovo1Btn.setText("" + randomChar);
        }*/

        Thread t = new Thread(new Runnable() {

            public void run() {
                while (threadRunning) {
                    char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                    slovo1Btn.setText("" + randomChar);
                }
            }
        });
        t.start();
    }

    public void stopOnClick (View v){
        switch (brojac){
            case 0:
                threadRunning = false;
                Thread t = new Thread(new Runnable() {
                public void run() {
                    while (!threadRunning) {
                        char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                        slovo2Btn.setText("" + randomChar);
                    }
                }
            });
                t.start();
               brojac++;
                break;
            case 1: threadRunning = true;
                Thread t1 = new Thread(new Runnable() {
                    public void run() {
                        while (threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo3Btn.setText("" + randomChar);
                        }
                    }
                });
                t1.start();
                brojac++;
                break;
            case 2: threadRunning = false;
                Thread t2 = new Thread(new Runnable() {
                    public void run() {
                        while (!threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo4Btn.setText("" + randomChar);
                        }
                    }
                });
                t2.start();
                brojac++;
                break;
            case 3: threadRunning = true;
                Thread t3 = new Thread(new Runnable() {
                    public void run() {
                        while (threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo5Btn.setText("" + randomChar);
                        }
                    }
                });
                t3.start();
                brojac++;
                break;
            case 4: threadRunning = false;
                Thread t4 = new Thread(new Runnable() {
                    public void run() {
                        while (!threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo6Btn.setText("" + randomChar);
                        }
                    }
                });
                t4.start();
                brojac++;
                break;
            case 5: threadRunning = true;
                Thread t5 = new Thread(new Runnable() {
                    public void run() {
                        while (threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo7Btn.setText("" + randomChar);
                        }
                    }
                });
                t5.start();
                brojac++;
                break;
            case 6: threadRunning = false;
                Thread t6 = new Thread(new Runnable() {
                    public void run() {
                        while (!threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo8Btn.setText("" + randomChar);
                        }
                    }
                });
                t6.start();
                brojac++;
                break;
            case 7: threadRunning = true;
                Thread t7 = new Thread(new Runnable() {
                    public void run() {
                        while (threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo9Btn.setText("" + randomChar);
                        }
                    }
                });
                t7.start();
                brojac++;
                break;
            case 8: threadRunning = false;
                Thread t8 = new Thread(new Runnable() {
                    public void run() {
                        while (!threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo10Btn.setText("" + randomChar);
                        }
                    }
                });
                t8.start();
                brojac++;
                break;
            case 9: threadRunning = true;
                Thread t9 = new Thread(new Runnable() {
                    public void run() {
                        while (threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo11Btn.setText("" + randomChar);
                        }
                    }
                });
                t9.start();
                brojac++;
                break;
            case 10: threadRunning = false;
                Thread t10 = new Thread(new Runnable() {
                    public void run() {
                        while (!threadRunning) {
                            char randomChar = randomLetters.charAt(r.nextInt(randomLetters.length()));
                            slovo12Btn.setText("" + randomChar);
                        }
                    }
                });
                t10.start();
                brojac++;
                break;
            case 11: threadRunning = true;
                brojac++;
                stopBtn.setEnabled(false);
                unosReciTxt.setText(""+slovo1Btn.getText().toString()+""+slovo2Btn.getText().toString());
                break;
        }
    }
}
答案

使用Handler对象从另一个线程更新UI元素。

请参阅此问题以获取示例How to textView.setText from Thread?

另一答案

我想看看你的错误日志。通过阅读代码我会说你的问题是你正在尝试从不是UI线程的线程更新UI项。

要快速测试并解决您的问题,只需将button.setText放在runonUIThread块中即可。要与另一个线程的UI线程进行通信,您可以使用Handler。

DOCS:https://developer.android.com/training/multiple-threads/communicate-ui https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable)

以上是关于Android - 线程问题(按钮中的随机字符切换,直到单击按钮)的主要内容,如果未能解决你的问题,请参考以下文章

通过单击 Android Studio 中的按钮在 webview 中获取随机 Youtube 视频

Android如何使用工具栏中的按钮切换以使用片段打开/关闭导航抽屉

按下按钮并转到 Xcode 中的随机视图

从另一个线程更改按钮图标/切换按钮

如何访问列表中的随机项目?

“再见RxJava,你好Flow”,Android线程切换哪家最强?