在doInBackground方法例外 [英] Exceptions in the doInBackground method

查看:120
本文介绍了在doInBackground方法例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有这样长的处理任务的应用程序

I have this long processing task in a app

据我了解,该方法doInBackground中的SwingWorker不
应该做的任何UI。不过,我做也有可能
抛出异常,我愿与错误信息给用户来处理。

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.

我应该怎么处理呢?

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)

更新结束

推荐答案

这里最重要的消息是这样的:
活动net.website.qt.mediaplayer.Main渗漏,最初这里添加窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40525ae0

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

这是一个pretty常见的错误和SO已经已经解决了几次:

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

<一个href=\"http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added\">Activity有最初添加泄露窗口

<一个href=\"http://stackoverflow.com/questions/5181508/activity-has-leaked-window-com-android-internal-policy-impl-phonewindowdecorvie\">Activity已泄漏,最初这里添加窗口com.android.internal.policy.impl.PhoneWindow$DecorView@46368a28

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

我是pretty肯定有问题的行是:

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 需要上下文作为第一个参数,而不是一个的AsyncTask 。在当您使用此情况下,这个其指向的AsyncTask 而不是一个上下文这是造成问题的原因。

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.

这篇关于在doInBackground方法例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆