
以下例外意味着什么;我该怎么解决?
这是代码:
Toast toast = Toast.makeText(mContext, "Something", Toast.LENGTH_SHORT);这是例外:
java.lang.RuntimeException: Can't create handler insIDe thread that has not called Looper.prepare() at androID.os.Handler.<init>(Handler.java:121) at androID.Widget.Toast.<init>(Toast.java:68) at androID.Widget.Toast.makeText(Toast.java:231)解决方法:
你是从工作线程调用它.您需要从主线程中调用Toast.makeText()(以及处理UI的大多数其他函数).例如,您可以使用处理程序.
在文档中查找Communicating with the UI Thread.简而言之:
// Set this up in the UI thread.mHandler = new Handler(Looper.getMainLooper()) { @OverrIDe public voID handleMessage(Message message) { // This is where you do your work in the UI thread. // Your worker tells you in the message what to do. }};voID workerThread() { // And this is how you call it from the worker thread: Message message = mHandler.obtainMessage(command, parameter); message.sendToTarget();}其他选择:
您可以使用AsyncTask,它适用于在后台运行的大多数事情.它有一些钩子,您可以调用它来指示进度,以及何时完成.
你也可以使用Activity.runOnUiThread().
总结以上是内存溢出为你收集整理的android – 无法在未调用Looper.prepare()的线程内创建处理程序全部内容,希望文章能够帮你解决android – 无法在未调用Looper.prepare()的线程内创建处理程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)