gpt4 book ai didi

android - 线程相关问题

转载 作者:搜寻专家 更新时间:2023-11-01 09:14:49 26 4
gpt4 key购买 nike

我正在尝试使用基本示例处理线程。但临近逼近。这是代码。

   public class ThreadExample extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Thread myThread=null;
Runnable runnable=new CountDownRunner();
myThread=new Thread(runnable);

myThread.start();
}
public void Work(){runOnUiThread(new Runnable() {
public void run() {


try{
EditText ed1= (EditText)findViewById(R.id.ed1);
Date dt = new Date(0);
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":"+minutes + ":"+ seconds;
ed1.setText(curTime);
}catch (Exception e) {

}
}
});
}
class CountDownRunner extends ThreadExample implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Work();
Toast.makeText(getApplicationContext(), "PIyush", Toast.LENGTH_LONG).show();

}
}

我检查了 DDMS 并得到了这个

03-25 00:51:44.145: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 00:54:59.515: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 00:55:44.586: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 00:57:47.935: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:01:22.805: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:02:00.255: DEBUG/dalvikvm(51): GC freed 16675 objects / 783752 bytes in 171ms
03-25 01:19:34.195: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:19:35.235: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:20:49.645: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:20:57.285: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:27:24.475: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:28:30.065: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:29:44.835: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:36:45.355: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:38:07.125: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:46:00.256: DEBUG/dalvikvm(51): GC freed 16800 objects / 782664 bytes in 205ms
03-25 01:46:58.495: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:50:45.075: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:58:26.595: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting
03-25 01:58:43.965: DEBUG/dalvikvm(51): threadid=15: bogus mon 1+0>0; adjusting

最佳答案

这是您代码的工作版本,它应该为您指明使编辑控件正常工作的正确方向:

public class ThreadExample extends Activity {
EditText ed1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText)findViewById(R.id.ed1);

Thread myThread=null;
Runnable runnable=new CountDownRunner();
myThread=new Thread(runnable);

myThread.start();
}

class CountDownRunner implements Runnable{
@Override
public void run() {
try {
Thread.sleep(10000);
}
catch (InterruptedException ex) {

}// TODO Auto-generated method stub
Work();
}

public void Work(){
try{
ed1.post(new Runnable() {
public void run() {
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":"+minutes + ":"+ seconds;
Toast.makeText(getApplicationContext(), "PIyush",
Toast.LENGTH_LONG).show();
ed1.setText(curTime);
}
});
}
catch (Exception e) {
Log.d("test", e.toString());
}
}
}
}

一些我不确定的事情:

  • 您的 Countdown runner 正在扩展 ThreadExample。我认为这会将 ThreadExample 中的所有成员都推到范围之外(这就是为什么我的版本没有)
  • 你的线程没有循环,我不确定你是否可以在创建完成之前发布到 UI(这就是为什么在我的运行函数开始时有一个延长的 sleep )。

关于android - 线程相关问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com