2
try {
    Get_Webpage obj = new Get_Webpage(url);
    directory_listings = obj.get_webpage_source();
} catch (Exception e) {
    finish();
    m_ProgressDialog.dismiss();
    Toast.makeText(this, "You have to be connected to the internet for this application to work", Toast.LENGTH_LONG).show();
}


02-28 22:23:21.055: E/AndroidRuntime(2170): FATAL EXCEPTION: AsyncTask #1
02-28 22:23:21.055: E/AndroidRuntime(2170): java.lang.RuntimeException: An error occured while executing doInBackground()
02-28 22:23:21.055: E/AndroidRuntime(2170):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.lang.Thread.run(Thread.java:1019)
02-28 22:23:21.055: E/AndroidRuntime(2170): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-28 22:23:21.055: E/AndroidRuntime(2170):     at android.os.Handler.<init>(Handler.java:121)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at android.widget.Toast.<init>(Toast.java:68)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at android.widget.Toast.makeText(Toast.java:231)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at net.website.qt.mediaplayer.Main.showToast(Main.java:414)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at net.website.qt.mediaplayer.Main.access$15(Main.java:413)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at net.website.qt.mediaplayer.Main$taskDoSomething.doInBackground(Main.java:199)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at net.website.qt.mediaplayer.Main$taskDoSomething.doInBackground(Main.java:1)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-28 22:23:21.055: E/AndroidRuntime(2170):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-28 22:23:21.055: E/AndroidRuntime(2170):     ... 4 more

Update

@Override 
protected void onPostExecute(List<Employee> result) 
{            
    m_Employee.clear();     
    m_Employee.addAll(result);   
    m_ProgressDialog.dismiss();
    m_adapter.notifyDataSetChanged();   
} 

I have this long processing task in a app

As I understand it, the method doInBackground in SwingWorker is not supposed to do anything with the UI. However, what I do in there may throw exceptions, which I would like to handle with error messages to the user.

how should i handle this?

private class taskDoSomething extends AsyncTask<Void, Void, List<Employee>> 
{ 

    @Override 
    protected List<Employee> doInBackground(Void... params) 
    { 
    String url = "http://ofertaweb.ro/android/sleepandlovemusic/list_files.php";

    try {
        Get_Webpage obj = new Get_Webpage(url);
        directory_listings = obj.get_webpage_source();
    } catch (Exception e) {
         Toast.makeText(this, "You have to be connected to the internet for this application to work", Toast.LENGTH_LONG).show();
       finish();
    }

UPDATE with logcat:

02-28 21:47:01.194: W/dalvikvm(1820): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-28 21:47:01.314: E/AndroidRuntime(1820): FATAL EXCEPTION: AsyncTask #1
02-28 21:47:01.314: E/AndroidRuntime(1820): java.lang.RuntimeException: An error occured while executing doInBackground()
02-28 21:47:01.314: E/AndroidRuntime(1820):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.lang.Thread.run(Thread.java:1019)
02-28 21:47:01.314: E/AndroidRuntime(1820): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-28 21:47:01.314: E/AndroidRuntime(1820):     at android.os.Handler.<init>(Handler.java:121)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at android.widget.Toast.<init>(Toast.java:68)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at android.widget.Toast.makeText(Toast.java:231)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at net.website.qt.mediaplayer.Main.showToast(Main.java:411)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at net.website.qt.mediaplayer.Main.access$14(Main.java:410)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at net.website.qt.mediaplayer.Main$taskDoSomething.doInBackground(Main.java:197)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at net.website.qt.mediaplayer.Main$taskDoSomething.doInBackground(Main.java:1)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-28 21:47:01.314: E/AndroidRuntime(1820):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-28 21:47:01.314: E/AndroidRuntime(1820):     ... 4 more
02-28 21:47:02.954: E/WindowManager(1820): Activity net.website.qt.mediaplayer.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40525ae0 that was originally added here
02-28 21:47:02.954: E/WindowManager(1820): android.view.WindowLeaked: Activity net.website.qt.mediaplayer.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40525ae0 that was originally added here
02-28 21:47:02.954: E/WindowManager(1820):  at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-28 21:47:02.954: E/WindowManager(1820):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-28 21:47:02.954: E/WindowManager(1820):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-28 21:47:02.954: E/WindowManager(1820):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.Dialog.show(Dialog.java:241)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
02-28 21:47:02.954: E/WindowManager(1820):  at net.website.qt.mediaplayer.Main.onCreate(Main.java:136)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-28 21:47:02.954: E/WindowManager(1820):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 21:47:02.954: E/WindowManager(1820):  at android.os.Looper.loop(Looper.java:123)
02-28 21:47:02.954: E/WindowManager(1820):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 21:47:02.954: E/WindowManager(1820):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 21:47:02.954: E/WindowManager(1820):  at java.lang.reflect.Method.invoke(Method.java:507)
02-28 21:47:02.954: E/WindowManager(1820):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 21:47:02.954: E/WindowManager(1820):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 21:47:02.954: E/WindowManager(1820):  at dalvik.system.NativeStart.main(Native Method)

UPDATE END

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

3 Answers3

2

doInBackground works on a background thread so you won't be able to update the UI in this method.

If you are looking to update the UI to the user, you need to implement onPostExecute in your ASyncTask to display a Dialog (or something similar) to inform the user if an error occured.

EDIT:

In your code example, you should not display the toast in doInBackground. Instead if you get an exception in doInBackground you can return null. In your onPostExecute, if your result is null, then display the toast.

triad
  • 20,407
  • 13
  • 45
  • 50
2

The important message here is this: Activity net.website.qt.mediaplayer.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40525ae0 that was originally added here

This is a pretty common error and has been addressed several times on SO already:

Activity has leaked window that was originally added

Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46368a28 that was originally added here

I'm pretty sure the offending line is:

     Toast.makeText(this, "You have to be connected to the internet for this application to work", Toast.LENGTH_LONG).show();

Toast.makeText takes a Context as its first argument and not an AsyncTask. In this case when you use this its pointing to the AsyncTask not to a Context which is causing the problem.

Community
  • 1
  • 1
slayton
  • 20,123
  • 10
  • 60
  • 89
  • okay so even after i comment the Toast.makeText line i still get the errors and i know why i am getting those error and its due to my code trying to access internet and it failed ... so thats why i wrap my code in `try and catch block` and in `OnPostexecute` I am doing progressDialog.dismiss();... i will updated my question. – Nick Kahn Feb 29 '12 at 03:13
0

if error happen, if you want to notify user in ui, you can use method publishProgress to pass error code or message to the ui, later method onProgressUpdate will be invoked, the onProgressUpdate can update ui.

suggestion read the android async task carefully

idiottiger
  • 5,147
  • 2
  • 25
  • 21