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
更新
@Override
protected void onPostExecute(List<Employee> result)
{
m_Employee.clear();
m_Employee.addAll(result);
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
我在应用程序中有这个漫长的处理任务
据我了解,SwingWorker中的方法doInBackground不是 应该对UI做任何事情。但是,我在那里做的可能 抛出异常,我想处理错误消息给用户。
我应该如何处理?
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();
}
使用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)
更新结束
答案 0 :(得分:2)
doInBackground
适用于后台线程,因此您无法在此方法中更新UI。
如果您要将UI更新为用户,则需要在ASyncTask
中实现onPostExecute以显示对话框(或类似内容),以便在发生错误时通知用户。
修改强>
在您的代码示例中,您不应在doInBackground
中显示toast。相反,如果您在doInBackground
中收到例外,则可以return null
。在onPostExecute
中,如果结果为null,则显示吐司。
答案 1 :(得分:2)
这里的重要信息是:
Activity net.website.qt.mediaplayer.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40525ae0 that was originally added here
这是一个非常常见的错误,已在SO上多次解决:
Activity has leaked window that was originally added
我很确定违规行是:
Toast.makeText(this, "You have to be connected to the internet for this application to work", Toast.LENGTH_LONG).show();
Toast.makeText
将Context
作为其第一个参数,而不是AsyncTask
。在这种情况下,当您使用this
时,AsyncTask
指向导致问题的Context
而不是{{1}}。
答案 2 :(得分:0)
如果发生错误,如果要通过ui通知用户,可以使用方法publishProgress
要将错误代码或消息传递给ui,将调用稍后的方法onProgressUpdate,onProgressUpdate
可以更新ui。