Android出现java.lang.RuntimeException: Can‘t toast on a thread that has not called Looper.prepare()

时间:2024-03-28 13:36:37

今天又是写代码的美好一天>_<
往常运行的好好的程序,今天运行在弹出吐司(Toast)时却出现了java.lang.RuntimeException: Can’t toast on a thread that has not called Looper.prepare(),ε=(´ο`*)))唉

Android出现java.lang.RuntimeException: Can‘t toast on a thread that has not called Looper.prepare()
   Process: com.qdsg.screen, PID: 23288
    java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
,程序在主线程中创建handler后会创建一个looper对象,而子线程却不会,所以我在我使用Toast前先执行Looper.prepare();使用Toast后执行 Looper.loop();

if (TextUtils.isEmpty( serverPort )) {
    Looper.prepare();
    getView().showToast( R.string.ServerPort_not_null );
    Looper.loop();
    getView().dismissLoginDialog();
    return;
}