Question

I added Yahoo weather library to m project. Then I tried to implement YahooWeatherInfoListener to the main fragment.

There is a problem.

Inside AsyncTask I can't cast YahooWeatherInfoListener to the fragment.

I think there is sth I'm doin wrong inside AsncTask. Please take a look:

import zh.wang.android.apis.yweathergetter4a.WeatherInfo;
import zh.wang.android.apis.yweathergetter4a.YahooWeather;
import zh.wang.android.apis.yweathergetter4a.YahooWeather.SEARCH_MODE;
import zh.wang.android.apis.yweathergetter4a.YahooWeatherInfoListener;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class HomeFragment extends Fragment implements YahooWeatherInfoListener {

public HomeFragment(){}
private TextView Temperature;
private YahooWeather mYahooWeather = YahooWeather.getInstance(5000, 5000, true);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    Temperature = (TextView) rootView.findViewById(R.id.txtLabel);
    new searchByGPS().execute();
    return rootView;
}
private class searchByGPS extends AsyncTask<Void, Void, Void>{
    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... unused) {
        mYahooWeather.setNeedDownloadIcons(true);
        mYahooWeather.setSearchMode(SEARCH_MODE.GPS);
        mYahooWeather.queryYahooWeatherByGPS(getActivity().getApplicationContext(), (YahooWeatherInfoListener) this);

        return null;
    }
    protected void onPostExecute(Void unused) {

    }
}
@Override
public void gotWeatherInfo(WeatherInfo weatherInfo) {
    // TODO Auto-generated method stub
    Temperature.setText(weatherInfo.getCurrentTempC());
}
}
Was it helpful?

Solution 2

protected Void doInBackground(Void... unused) {
        mYahooWeather.setNeedDownloadIcons(true);
        mYahooWeather.setSearchMode(SEARCH_MODE.GPS);
        mYahooWeather.queryYahooWeatherByGPS(getActivity().getApplicationContext(), (YahooWeatherInfoListener) HomeFragment.this);

        return null;
    }

You have to change your this to HomeFragment.this because this is directly connected to your AsyncTask.

OTHER TIPS

That's because in

(YahooWeatherInfoListener) this

this refers to the AsyncTask. Instead, you need to use:

(YahooWeatherInfoListener) HomeFragment.this

The cleaner way to implement this would be:

private class SearchByGPSTask extends AsyncTask<Void, Void, Void>{

    private YahooWeatherInfoListener mListener;

    public SearchByGPSTask( YahooWeatherInfoListener listener ) {
        super();
        mListener = listener;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... unused) {
        mYahooWeather.setNeedDownloadIcons(true);
        mYahooWeather.setSearchMode(SEARCH_MODE.GPS);
        mYahooWeather.queryYahooWeatherByGPS(getActivity().getApplicationContext(), mListener );

        return null;
    }
    protected void onPostExecute(Void unused) {

    }
}

(note: Classes are generally uppercase, and better nouns -- "searchByGPS" sounds like a method, while "SearchByGPSTask" indicates that it's a class meant to do something.)

With this line mYahooWeather.queryYahooWeatherByGPS(getActivity().getApplicationContext(), (YahooWeatherInfoListener) this);, you are trying to cast the AsyncTask to a YahooWeatherInfoListener. Use HomeFragment.this instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top