无法在未调用 Looper.prepare() 的线程 Thread[Thread-5,5,main] 中创建处理程序 [重复]
Posted
技术标签:
【中文标题】无法在未调用 Looper.prepare() 的线程 Thread[Thread-5,5,main] 中创建处理程序 [重复]【英文标题】:Can't create handler inside thread Thread[Thread-5,5,main] that has not called Looper.prepare() [duplicate] 【发布时间】:2019-07-22 00:26:19 【问题描述】:以下异常是什么意思;我该如何解决? 这是代码:
if (response.isSuccessful())
String message = response.body().string();
JSONObject jsonObject = new JSONObject(message);
new AlertDialog.Builder(mcontext)
.setTitle("title:")
.setMessage(TmpPwd)
.setPositiveButton("close", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
)
.show();
这是个例外:
W/System.err: java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-5,5,main] that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:205)
【问题讨论】:
是的,您可能在非 UI 线程上调用此代码 【参考方案1】:在您的onResponse
内显示您的对话框,如下面的 UI 线程内
activity.runOnUiThread(new Runnable()
public void run()
new AlertDialog.Builder(mcontext)
.setTitle("title:")
.setMessage(TmpPwd)
.setPositiveButton("close", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
)
.show();
);
【讨论】:
【参考方案2】:Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable()
public void run()
//here show dialog
);
【讨论】:
以上是关于无法在未调用 Looper.prepare() 的线程 Thread[Thread-5,5,main] 中创建处理程序 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
无法在未调用 Looper.prepare() 的线程内创建处理程序
无法在未调用 Looper.prepare() 的线程内创建处理程序
无法在未调用 Looper.prepare()-Alertdialogbox 的线程内创建处理程序 [重复]
无法在未调用 Looper.prepare() Graphhopper 的线程内创建处理程序