【问题标题】:Can't cast library class to fragment无法将库类转换为片段
【发布时间】:2014-03-17 15:17:51
【问题描述】:

我在 m 项目中添加了 Yahoo 天气库。然后我尝试在主片段中实现 YahooWeatherInfoListener。

有问题。

AsyncTask 内部,我无法将YahooWeatherInfoListener 强制转换为片段。

我认为我在 AsncTask 中做错了什么。请看:

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());
}
}

【问题讨论】:

  • 您想使用HomeFragment.this 而不是this

标签: android android-fragments android-asynctask android-library


【解决方案1】:

那是因为在

(YahooWeatherInfoListener) this

this 指的是AsyncTask。相反,您需要使用:

(YahooWeatherInfoListener) HomeFragment.this

更简洁的实现方式是:

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) {

    }
}

(注意:类通常是大写的,并且是更好的名词——“searchByGPS”听起来像是一种方法,而“SearchByGPSTask”表示它是一个旨在做某事的类。)

【讨论】:

  • 现在出现此错误:“无法在未调用 looper.prepare() 的 looper 内创建处理程序;”
  • 这似乎与您的第一个问题无关。 logcat 是指向您自己的代码还是在雅虎的代码中?
  • 这个问题解决了。现在它在片段中找不到 textview。
【解决方案2】:
protected Void doInBackground(Void... unused) {
        mYahooWeather.setNeedDownloadIcons(true);
        mYahooWeather.setSearchMode(SEARCH_MODE.GPS);
        mYahooWeather.queryYahooWeatherByGPS(getActivity().getApplicationContext(), (YahooWeatherInfoListener) HomeFragment.this);

        return null;
    }

您必须将您的 this 更改为 HomeFragment.this,因为这直接连接到您的 AsyncTask。

【讨论】:

  • 现在出现此错误:“无法在未调用 looper.prepare() 的 looper 内创建处理程序;”
  • 所以一个线程中有一个线程。如示例所示,您这次不必使用 asynctask github.com/zh-wang/YWeatherGetter4a/blob/master/sample_app/src/…
  • 你的意思是我必须在没有 AsyncTask 的情况下使用进度对话框?难道我没有得到主线程异常错误(因为 GPS 正在搜索位置)?
  • 解决了!但是一个有趣的错误:应用程序出现但几秒钟后(我认为在检索温度后)应用程序崩溃并出现此错误:“找不到字符串资源 ID:0 * 14 ...)”似乎找不到文本视图来插入温度.但是我检查了 textview 是否存在并且 ID 是正确的。我什至在片段布局中添加了另一个文本视图。
  • 我认为这是问题所在,因为您想将 int 设置为文本,因此 Temperature.setText(14) 将查找字符串 ressource 14,只需将其更改为 Temperature.setText("" + 14) 它将显示 14
【解决方案3】:

通过mYahooWeather.queryYahooWeatherByGPS(getActivity().getApplicationContext(), (YahooWeatherInfoListener) this); 这一行,您正在尝试将AsyncTask 转换为YahooWeatherInfoListener。请改用HomeFragment.this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2015-11-05
    • 1970-01-01
    • 2018-02-26
    • 2015-04-09
    • 2013-03-21
    相关资源
    最近更新 更多