开发者

Thread related issue

开发者 https://www.devze.com 2023-02-20 03:30 出处:网络
I am trying my hand on thread with basic example. But facing force close. Here\'s the code. public class ThreadExample extends Activity {

I am trying my hand on thread with basic example. But facing force close. Here's the code.

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

    }
}

I checked the DDMS and getting this

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


This is a working version of your code, which should point you in the right direction for getting your edit control working:

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

A few things I wasn't sure about:

  • Your Countdown runner was extending ThreadExample. I think this pushes all of your members from ThreadExample out of scope (which is why my version doesn't)
  • Your thread doesn't loop, I'm not sure if you can post to the UI, before it's finished being created (which is why there's an extended sleep at the start of my run function).


The line EditText myText=(EditText)findViewById(R.id.ed1); is called before setContentView() so you probably get a null pointer exception, instead, you can do it in this way:

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);
    ...


You are not allowed (and should not) to call findViewById or communicate with any view from another thread than the UI thread. The stack trace will say that as well.

Check how an asyncTask works or just use the use the runOnUIThread method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号