-2

Toast在 android 中遇到此错误,无法弄清楚:

05-23 03:22:33.415: E/AndroidRuntime(706): FATAL EXCEPTION: AsyncTask #1
05-23 03:22:33.415: E/AndroidRuntime(706): java.lang.RuntimeException: An error occured while executing doInBackground()
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.lang.Thread.run(Thread.java:864)
05-23 03:22:33.415: E/AndroidRuntime(706): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.os.Handler.<init>(Handler.java:121)
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.widget.Toast$TN.<init>(Toast.java:317)
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.widget.Toast.<init>(Toast.java:91)
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.widget.Toast.makeText(Toast.java:233)
05-23 03:22:33.415: E/AndroidRuntime(706):  at com.example.devicecontrolpanel.TestActivity$DeviceSearcher.executed(TestActivity.java:482)
05-23 03:22:33.415: E/AndroidRuntime(706):  at com.example.devicecontrolpanel.TestActivity$DeviceSearcher.doInBackground(TestActivity.java:289)
05-23 03:22:33.415: E/AndroidRuntime(706):  at com.example.devicecontrolpanel.TestActivity$DeviceSearcher.doInBackground(TestActivity.java:1)
05-23 03:22:33.415: E/AndroidRuntime(706):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-23 03:22:33.415: E/AndroidRuntime(706):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-23 03:22:33.415: E/AndroidRuntime(706):  ... 5 more

代码太大所以我发布了它的一部分:

if(!set.getCheckComputer() && !set.getCheckFlyport() && !set.getCheckRaspberry())
            executed(); //Line number: 289

public void executed()
    {
        DataBaseAdapter db = new DataBaseAdapter(getApplicationContext());
        List<DeviceInformation> oldDevices;
        if(deviceList.size()>0)
            oldDevices = db.getNotAllDevices(deviceList);
        else
            oldDevices = null;
        if(oldDevices!=null)
        {
            ...
            ...
        }
        else
        {
            TableRow tr = new TableRow(getApplicationContext());
            tr.setLayoutParams(layoutParam);
            TextView tv = new TextView(getApplicationContext());
            tv.setTextSize(17);
            tv.setLayoutParams(layoutParam);
            if(rowCounter!=0)
                tv.setText("No device were added before");
            tv.setVisibility(View.VISIBLE);
            tv.setTextColor(Color.WHITE);
            tr.addView(tv);
            tr.setBackgroundColor(Color.BLACK);
            tr.setVisibility(View.VISIBLE);
            deviceTable.addView(tr);
            Toast.makeText(getApplicationContext(), "No Devices Found", Toast.LENGTH_LONG).show();
                            //Error in above line
        }
    }
4

1 回答 1

4

您正在尝试从 UI 线程创建 Toast,这是被禁止的。利用

  runOnUiThread(new Runnable() {
      public void run() {
          Toast.makeText(getApplicationContext(), "No Devices Found", Toast.LENGTH_LONG).show();
   }});

onPostExecute或从方法中调用它

于 2013-05-23T02:17:15.473 回答