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 的线程内创建


基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01