Can't create handler inside thread that has not called Looper.prepare() Android

后端 未结 2 381
轮回少年
轮回少年 2020-12-21 22:50

I am using AsyncTask to call yahooweather API. Following is the code:


public class myactivity extends Activity {
    final String yahooapisBase = \"http:         


        
相关标签:
2条回答
  • 2020-12-21 23:03

    you are calling

    Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();
    

    in QueryYahooWeather which is being called in doInBackground.

    You can not call UI calls from background thread.

    0 讨论(0)
  • 2020-12-21 23:23

    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.

    NOTE : if you want to known what's going on in background then use Log instead of Toast's

    EDIT:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题