-4

我有以下代码,用于线程。当我运行应用程序时,我得到以下异常。

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

我如何为这段代码实现处理程序?

4

2 回答 2

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();
于 2013-04-10T04:16:53.057 回答
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();
        }
      }}); 

希望这可以帮助你。

于 2013-04-10T04:17:44.313 回答