Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';t将库类转换为片段_Android_Android Fragments_Android Asynctask_Android Library - Fatal编程技术网

Android Can';t将库类转换为片段

Android Can';t将库类转换为片段,android,android-fragments,android-asynctask,android-library,Android,Android Fragments,Android Asynctask,Android Library,我将Yahoo气象库添加到m项目中。然后我尝试将YahooWeatherInfoListener实现到主片段 有一个问题 在AsyncTask内部,我无法将YahooWeatherInfoListener强制转换为片段 我想在AsncTask里面我做错了什么。请看一看: import zh.wang.android.apis.yweathergetter4a.WeatherInfo; import zh.wang.android.apis.yweathergetter4a.YahooWeathe

我将Yahoo气象库添加到m项目中。然后我尝试将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());
}
}
导入zh.wang.android.api.yweathergetter4a.WeatherInfo;
导入zh.wang.android.api.yweathergetter4a.YahooWeather;
导入zh.wang.android.api.yweathergetter4a.YahooWeather.SEARCH_模式;
导入zh.wang.android.api.yweathergetter4a.YahooWeatherInfoListener;
导入android.app.Fragment;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
公共类HomeFragment扩展片段实现YahooWeatherInfoListener{
公共HomeFragment(){}
私有文本视图温度;
私有YahooWeather-myahoweather=YahooWeather.getInstance(50005000,true);
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u home,container,false);
温度=(TextView)rootView.findViewById(R.id.txtLabel);
新建searchByGPS().execute();
返回rootView;
}
专用类searchByGPS扩展异步任务{
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的空位背景(空位…未使用){
myahoweather.setNeedDownloadIcons(true);
myahoweather.setSearchMode(搜索模式.GPS);
myahoowather.queryyahowatherbygps(getActivity().getApplicationContext(),(YahooWeatherInfoListener)this);
返回null;
}
受保护的void onPostExecute(未使用的void){
}
}
@凌驾
公共void gotWeatherInfo(WeatherInfo WeatherInfo){
//TODO自动生成的方法存根
Temperature.setText(weatherInfo.getCurrentTempC());
}
}
您必须将
更改为
HomeFragment。此
,因为它直接连接到您的任务

您必须将
更改为
HomeFragment。此
,因为它直接连接到您的任务

您必须将
更改为
HomeFragment。此
,因为它直接连接到您的任务

您必须将
this
更改为
HomeFragment。这是因为它直接连接到异步任务。

这是因为

(YahooWeatherInfoListener) this
指的是
异步任务
。相反,您需要使用:

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

    }
}
私有类SearchByGPSTask扩展异步任务{
私人YahooWeatherInfoListener;
公共搜索BYGPSTASK(YahooWeatherInfoListener侦听器){
超级();
mListener=监听器;
}
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的空位背景(空位…未使用){
myahoweather.setNeedDownloadIcons(true);
myahoweather.setSearchMode(搜索模式.GPS);
myahoweather.queryahoweatherbygps(getActivity().getApplicationContext(),mListener);
返回null;
}
受保护的void onPostExecute(未使用的void){
}
}
(注意:类通常是大写的,更好的名词--“searchByGPS”听起来像一个方法,而“SearchByGPSTask”表示它是一个用来做某事的类。)

这是因为

(YahooWeatherInfoListener) this
指的是
异步任务
。相反,您需要使用:

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

    }
}
私有类SearchByGPSTask扩展异步任务{
私人YahooWeatherInfoListener;
公共搜索BYGPSTASK(YahooWeatherInfoListener侦听器){
超级();
mListener=监听器;
}
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的空位背景(空位…未使用){
myahoweather.setNeedDownloadIcons(true);
myahoweather.setSearchMode(搜索模式.GPS);
myahoweather.queryahoweatherbygps(getActivity().getApplicationContext(),mListener);
返回null;
}
受保护的void onPostExecute(未使用的void){
}
}
(注意:类通常是大写的,更好的名词--“searchByGPS”听起来像一个方法,而“SearchByGPSTask”表示它是一个用来做某事的类。)

这是因为

(YahooWeatherInfoListener) this
指的是
异步任务
。相反,您需要使用:

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

    }
}
私有类SearchByGPSTask扩展异步任务{
私人YahooWeatherInfoListener;
公共搜索BYGPSTASK(YahooWeatherInfoListener侦听器){
超级();
mListener=监听器;
}
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的空位背景(空位…未使用){
myahoweather.setNeedDownloadIcons(true);
myahoweather.setSearchMode(搜索模式.GPS);
myahoweather.queryahoweatherbygps(getActivity().getApplicationContext(),mListener);
返回null;
}
受保护的void onPostExecute(未使用的void){
}
}
(注意:类通常是大写的,更好的名词--“searchByGPS”听起来像一个方法,而“SearchByGPSTask”表示它是一个用来做某事的类。)

这是因为

(YahooWeatherInfoListener) this
指的是
异步任务
。相反,您需要使用:

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

    }
}
私有类SearchByGPSTask扩展异步任务{
私人YahooWeatherInfoListener;
公共搜索BYGPSTASK(YahooWeatherInfoListener侦听器){
超级();
mListener=监听器;
}
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的空doInBack