线程相关问题

时间:2011-03-24 19:29:20

标签: android

我正在尝试使用基本示例的线程。但面临力量关闭。 这是代码。

   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

3 个答案:

答案 0 :(得分:2)

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

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

我不确定的一些事情:

  • 你的倒计时跑步者正在扩展ThreadExample。我认为这会将ThreadExample中的所有成员推出范围(这就是为什么我的版本没有)
  • 你的线程没有循环,我不确定你是否可以在创建完成之前发布到UI(这就是为什么在我的run函数开始时有一个延长的睡眠)。

答案 1 :(得分:1)

EditText myText=(EditText)findViewById(R.id.ed1);之前调用行setContentView(),因此您可能会得到一个空指针异常,相反,您可以这样做:

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

答案 2 :(得分:0)

您不允许(也不应该)调用findViewById或与除UI线程之外的其他线程的任何视图进行通信。堆栈跟踪也会这样说。

检查asyncTask的工作方式,或者只使用runOnUIThread方法。