java.lang.RuntimeException:Can't toast on a thread that has not called Looper.prepare()

项目刚上线,在友盟统计的数据看到闪退率达到2%以上,我擦。

    io.reactivex.c.f: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
 

java.lang.RuntimeException:Can't toast on a thread that has not called Looper.prepare()



    io.reactivex.c.f: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()

问题是在子线程中去进行Toast操作。

怎么解决呢。只有在ToastUtils中直接使用主线程。

例如

   /**
     * 简单Toast 消息弹出
     */
    public static void show(final String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        // 判断是否在主线程
        if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
            QsHelper.getInstance().getThreadHelper().getMainThread().execute(new Runnable() {
                @Override public void run() {
                    showToast(QsHelper.getInstance().getApplication(), msg, Toast.LENGTH_SHORT);
                }
            });
        } else {
            showToast(QsHelper.getInstance().getApplication(), msg, Toast.LENGTH_SHORT);
        }
    }
发布了57 篇原创文章 · 获赞 40 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37492806/article/details/101347106