无法在尚未调用 Looper.prepare() Android 的线程内创建处理程序 [英] Can't create handler inside thread that has not called Looper.prepare() Android

查看:27
本文介绍了无法在尚未调用 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 的线程内创建处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆