实现线程处理程序

时间:2013-04-10 04:03:17

标签: android multithreading

对于一个帖子,我有以下代码。当我运行应用程序时,我得到以下异常。

04-10 09:16:29.399: E/AndroidRuntime(14847): FATAL EXCEPTION: Thread-10
04-10 09:16:29.399: E/AndroidRuntime(14847): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-10 09:16:29.399: E/AndroidRuntime(14847):    at android.os.Handler.<init>(Handler.java:121)
04-10 09:16:29.399: E/AndroidRuntime(14847):    at android.widget.Toast.<init>(Toast.java:76)
04-10 09:16:29.399: E/AndroidRuntime(14847):    at android.widget.Toast.makeText(Toast.java:251)
04-10 09:16:29.399: E/AndroidRuntime(14847):    at com.mobilevoiceapps.speeddial.Class_Add_Contact$1.run(Class_Add_Contact.java:88)
04-10 09:16:29.399: E/AndroidRuntime(14847):    at java.lang.Thread.run(Thread.java:1019)

这是我的代码:::

Thread myThread = new Thread(new Runnable(){
    @Override
    public void run()
    {
        try
        {
          while(!isLoaded)
          {
              Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts.. Please Wait !!!", Toast.LENGTH_LONG).show();
              wait(5000);
          }
        }
        catch (Exception e)
        {

        }
        finally{
            // Exception at below line
            Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts Complete", Toast.LENGTH_LONG).show();
        }
    }
});

myThread.start();

如何为此代码实现Handler?

2 个答案:

答案 0 :(得分:0)

以下是我改变它的方式,并且有效。

Thread myThread = new Thread(new Runnable(){
            Class_Add_Contact cladd = new Class_Add_Contact();

            @Override
            public void run()
            {
                try
                {
                  while(!isLoaded)
                  {
                      Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts.. Please Wait !!!", Toast.LENGTH_LONG).show();
                      wait(5000);
                  }
                }
                catch (Exception e)
                {

                }
                finally{

                    //Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts Complete", Toast.LENGTH_LONG).show();

                    cladd.runOnUiThread(new Runnable() {
                          public void run() {
                            Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts Complete", Toast.LENGTH_SHORT).show();
                          }
                        });
                }
            }
        });

        myThread.start();

答案 1 :(得分:0)

尝试

    runOnUiThread(new Runnable() { @Override public void run() { 
    try
        {
          while(!isLoaded)
          {
              Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts.. Please Wait     !!!", Toast.LENGTH_LONG).show();
              wait(5000);
          }
        }
        catch (Exception e)
        {

        }
        finally{
            // Exception at below line
            Toast.makeText(Class_Add_Contact.this, "Retrieving Contacts Complete", Toast.LENGTH_LONG).show();
        }
      }}); 

希望这可以帮助你。