-2

I am getting this error with my Toast in android, not able to figure out this:

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

The code is too big So I am posting parts of it:

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
        }
    }
Sharda Singh
  • 727
  • 3
  • 10
  • 19
  • This question has been answered on SO multiple times. So search before posting a question. http://stackoverflow.com/questions/5747645/problem-with-toast-in-asynctask-method-call http://stackoverflow.com/questions/13790351/how-to-show-toast-in-asynctask-in-doinbackground – Siddharth May 23 '13 at 02:53
  • you cant change UI in main thread.. put your Toast in PostExecute method or in RunOnUi Thread.. – Sagar Maiyad May 23 '13 at 05:11

1 Answers1

4

You are trying to create Toast out of UI Thread, that is prohibited. Use

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

or call it from onPostExecute method

ArturSkowronski
  • 1,722
  • 13
  • 17