LoaderManager 抛出不兼容的类型错误
LoaderManager throwing incompatible types error
我在我创建的片段之一中调用 AsyncTaskLoader
。我已导入 import android.app.LoaderManager;
,但在调用此方法 LoaderManager manager = getLoaderManager();
时出现错误。我的主 activity 中也有同样的事情发生,但它没有抛出任何错误。
它还要求我导入此 android.support.v4.app.LoaderManager
而不是 android.support.v4.app.LoaderManager
。当我这样做时,它显示 getLoaderManager()
已弃用。
我的FragmentExchangeList.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class FragmentExchangeList extends Fragment implements
LoaderManager.LoaderCallbacks<ArrayList<ExchangeListData>> {
private List<ExchangeListData> mExchangeListData;
private ListView mExchangeListView;
private AutoCompleteTextView mSetbaseCurrency;
private String linkForCurrencyList;
private static final int CURRENCY_List_LOADER_ID = 2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_exchange_list, container, false);
mExchangeListData = new ArrayList<ExchangeListData>();
mExchangeListView = rootView.findViewById(R.id.exchange_list_view);
mSetbaseCurrency = rootView.findViewById(R.id.set_base_currency);
linkForCurrencyList = "https://ratesapi.io/api/latest?base="+mSetbaseCurrency.getText().toString();
LoaderManager manager = getLoaderManager();
manager.initLoader(CURRENCY_List_LOADER_ID, null, this);
return rootView;
}
@Override
public Loader<ArrayList<ExchangeListData>> onCreateLoader(int i, Bundle bundle) {
return new ExchangeListBackground(getContext(), linkForCurrencyList);
}
@Override
public void onLoadFinished(Loader<ArrayList<ExchangeListData>> loader, ArrayList<ExchangeListData> data) {
mExchangeListData.clear();
if(data != null && !data.isEmpty()){
mExchangeListData.addAll(data);
}
}
@Override
public void onLoaderReset(Loader<ArrayList<ExchangeListData>> loader) {
mExchangeListData.clear();
}
}
错误是error: incompatible types: android.support.v4.app.LoaderManager cannot be converted to android.app.LoaderManager
删除 import android.app.LoaderManager;
并添加此行 import android.support.v4.app.LoaderManager
我以前遇到过同样的问题。您正在使用的 fragment
来自支持库,因此您也需要将 android.support.v4.app.LoaderManager
包用于 LoadManager
。
但是搜索关于弃用,是因为google 弃用了所有加载程序堆栈,他们建议使用 Jobs。因此,要解决您的问题,请使用已弃用的包并在 Android Studio not be annoying.
中添加一条禁止弃用的评论
我在我创建的片段之一中调用 AsyncTaskLoader
。我已导入 import android.app.LoaderManager;
,但在调用此方法 LoaderManager manager = getLoaderManager();
时出现错误。我的主 activity 中也有同样的事情发生,但它没有抛出任何错误。
它还要求我导入此 android.support.v4.app.LoaderManager
而不是 android.support.v4.app.LoaderManager
。当我这样做时,它显示 getLoaderManager()
已弃用。
我的FragmentExchangeList.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class FragmentExchangeList extends Fragment implements
LoaderManager.LoaderCallbacks<ArrayList<ExchangeListData>> {
private List<ExchangeListData> mExchangeListData;
private ListView mExchangeListView;
private AutoCompleteTextView mSetbaseCurrency;
private String linkForCurrencyList;
private static final int CURRENCY_List_LOADER_ID = 2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_exchange_list, container, false);
mExchangeListData = new ArrayList<ExchangeListData>();
mExchangeListView = rootView.findViewById(R.id.exchange_list_view);
mSetbaseCurrency = rootView.findViewById(R.id.set_base_currency);
linkForCurrencyList = "https://ratesapi.io/api/latest?base="+mSetbaseCurrency.getText().toString();
LoaderManager manager = getLoaderManager();
manager.initLoader(CURRENCY_List_LOADER_ID, null, this);
return rootView;
}
@Override
public Loader<ArrayList<ExchangeListData>> onCreateLoader(int i, Bundle bundle) {
return new ExchangeListBackground(getContext(), linkForCurrencyList);
}
@Override
public void onLoadFinished(Loader<ArrayList<ExchangeListData>> loader, ArrayList<ExchangeListData> data) {
mExchangeListData.clear();
if(data != null && !data.isEmpty()){
mExchangeListData.addAll(data);
}
}
@Override
public void onLoaderReset(Loader<ArrayList<ExchangeListData>> loader) {
mExchangeListData.clear();
}
}
错误是error: incompatible types: android.support.v4.app.LoaderManager cannot be converted to android.app.LoaderManager
删除 import android.app.LoaderManager;
并添加此行 import android.support.v4.app.LoaderManager
我以前遇到过同样的问题。您正在使用的 fragment
来自支持库,因此您也需要将 android.support.v4.app.LoaderManager
包用于 LoadManager
。
但是搜索关于弃用,是因为google 弃用了所有加载程序堆栈,他们建议使用 Jobs。因此,要解决您的问题,请使用已弃用的包并在 Android Studio not be annoying.
中添加一条禁止弃用的评论