一卷旌收千骑虏,万全身出百重围。这篇文章主要讲述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。
【Android - 线程问题(按钮中的随机字符切换,直到单击按钮)】DOCS:https://developer.android.com/training/multiple-threads/communicate-ui https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable)
推荐阅读
- 在循环中使用Realm或在Android中插入列表
- Android postdelayed处理程序无法启动运行
- 为什么我需要Android Studio中的GitHub中的Tag()
- 在Bootstrap 4中定义固定宽度input-group-append div
- 如何从图像Android应用程序中提取文本
- Android,无法使用WebView上传图像
- 在Glide for Android中离线时加载已获取的图像
- android减少摄像头捕获图像的文件大小小于500 kb
- Applescript制作新文件夹