Can#39;t create handler inside thread that has not called Looper.prepare() Android(无法在尚未调用 Looper.prepare() Android 的线程内创建处理程序)
问题描述
我正在使用 AsyncTask 调用 yahooweather API.代码如下:
I am using AsyncTask to call yahooweather API. Following is the code:
public class myactivity extends Activity {
final String yahooapisBase = "http://query.yahooapis.com/v1/public/yql?q=select*from%20geo.places%20where%20text=";
final String yahooapisFormat = "&format=xml";
String yahooAPIsQuery;
}
调试代码后发现yahooAPI调用成功,可以在QueryYahooWeather函数中看到XML响应.但是一旦这个函数的执行完成,就会抛出一个异常:无法在没有调用 Looper.prepare() 的线程内创建处理程序
After debugging the code I found that the yahooAPI call is successfull and I can see the XML response in QueryYahooWeather function. But as soon as execution of this function completes an exception has been thrown:
Can't create handler inside thread that has not called Looper.prepare()
请帮帮我.
推荐答案
从 QueryYahooWeather 方法中移除所有 Toast,因为该方法是从 doInBackground(Object... params) AsyncTask 并且你不能从后台线程访问像 Toast 这样的 Ui 元素(也是一个 Ui 元素).
Remove all Toast's from QueryYahooWeather method because this method is called from doInBackground(Object... params) of AsyncTask and you can not Access Ui elements like Toast(also an Ui element)from background Thread.
注意:如果您想知道后台发生了什么,请使用 日志 而不是 Toast 的
NOTE : if you want to known what's going on in background then use Log instead of Toast's
将 doInBackground 更改为:
Change doInBackground as :
@Override
protected String doInBackground(Object... params) {
// TODO Auto-generated method stub
String strresult="";
try {
Log.i("my label", "entering in doInBackground");
Log.i("params[0]", params[0].toString());
strresult= QueryYahooWeather(params[0].toString());
Log.i("strresult result ::: ", strresult);
} catch (Exception e) {
Log.i("my label", e.toString());
return null;
}
return strresult;
}
这篇关于无法在尚未调用 Looper.prepare() Android 的线程内创建处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在尚未调用 Looper.prepare() Android 的线程内创建
基础教程推荐
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Maven:无效的目标版本:10 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
