case1: Can't create handler inside thread that has not called Looper.prepare()

at android.widget.Toast.makeText(Toast.java:252)

11-07 16:10:51.538: E/AndroidRuntime(24455): FATAL EXCEPTION: pool-1-thread-1
11-07 16:10:51.538: E/AndroidRuntime(24455): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-07 16:10:51.538: E/AndroidRuntime(24455):    at android.os.Handler.<init>(Handler.java:121)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at android.widget.Toast$TN.<init>(Toast.java:364)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at android.widget.Toast.<init>(Toast.java:98)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at android.widget.Toast.makeText(Toast.java:252)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at com.tvie.ivideo.util.UIUtils.showToast(UIUtils.java:56)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at com.tvie.ivideo.util.CommonUtils.isDownloadAvailable(CommonUtils.java:196)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at com.tvie.ivideo.download.logic.DownTask.run(DownTask.java:89)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-07 16:10:51.538: E/AndroidRuntime(24455):    at java.lang.Thread.run(Thread.java:856)

case2: Can't create handler inside thread that has not called Looper.prepare()

at android.os.Handler.<init>(Handler.java:121)

11-07 16:15:53.808: E/AndroidRuntime(25044): FATAL EXCEPTION: pool-1-thread-1
11-07 16:15:53.808: E/AndroidRuntime(25044): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-07 16:15:53.808: E/AndroidRuntime(25044):    at android.os.Handler.<init>(Handler.java:121)
11-07 16:15:53.808: E/AndroidRuntime(25044):    at com.tvie.ivideo.util.CommonUtils$1.<init>(CommonUtils.java:197)
11-07 16:15:53.808: E/AndroidRuntime(25044):    at com.tvie.ivideo.util.CommonUtils.isDownloadAvailable(CommonUtils.java:197)
11-07 16:15:53.808: E/AndroidRuntime(25044):    at com.tvie.ivideo.download.logic.DownTask.run(DownTask.java:89)
11-07 16:15:53.808: E/AndroidRuntime(25044):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-07 16:15:53.808: E/AndroidRuntime(25044):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-07 16:15:53.808: E/AndroidRuntime(25044):    at java.lang.Thread.run(Thread.java:856)

以上两种情况出现的错误是由于在线程运行过程中直接显示Toast和创建Handler。

参考链接:http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare

You're calling it from a worker thread. You need to call Toast.makeText() (and most other functions dealing with the UI) from within the main thread.

以下方法可以避免发生这样的错误,先看经过验证正确的实现

在线程的run方法中调用static方法

@Override
public void run() {
    if(!CommonUtils.isDownloadAvailable())  return;

static方法定义在CommonUtils类中

/**
 * 判断是否允许下载
 * @return
 */
public static boolean isDownloadAvailable() {
    final Context context = TvieApplication.getContext();
    if(!NetUtil.hasNetwork(context)) {
        TvieApplication.getIVideoApp().showToast("网络不给力");
        return false;
    }
    if (!SettingSharedPreference.getInstance().getCacheWith2G3G()
            && !NetUtil.hasWifiNetWork(context)) {
        TvieApplication.getIVideoApp().showToast("禁止2G/3G下载操作");
        return false;
    }
    return true;
}

在TvieApplication中实现showToast(String)方法用以显示Toast

public static TvieApplication getIVideoApp() {
    return (TvieApplication) getInstance();
}
private Handler mHandler = new Handler() {
                                                                                               
    public void handleMessage(android.os.Message msg) {
        Toast.makeText(getContext(), (String)msg.obj, Toast.LENGTH_SHORT).show();
    }
};
public void showToast(String text) {
    mHandler.obtainMessage(0, text).sendToTarget();
}