这个问题与通过带有Java的android studio进行android开发有关。
当ListViewLoader调用getLoaderManager接受“ this”上下文时,我遇到了麻烦(扩展了ListActivity实现LoaderManager.LoaderCallbacks)。
我基本上是在以下链接中复制并粘贴代码:https://developer.android.com/guide/topics/ui/layout/listview#java。我在这里(getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) implements LoaderManager.LoaderCallbacks<Cursor>)阅读了答案。我试图确保我的导入是正确的,并且我的最小sdk是15,所以这应该不是问题。我尝试使用getSupportLoaderManager,而android studio无法解决它。我不太了解答案对于片段的含义。但是,当我从官方文档中复制代码时,我认为不需要。
我在构建时遇到的错误如下:
error: method initLoader in class LoaderManager cannot be applied to given types;
required: int,Bundle,LoaderCallbacks<D>
found: int,<null>,ListViewLoader
reason: cannot infer type-variable(s) D
(argument mismatch; ListViewLoader cannot be converted to LoaderCallbacks<D>)
where D is a type-variable:
D extends Object declared in method <D>initLoader(int,Bundle,LoaderCallbacks<D>)
我试图强制类型转换,但这只会导致类似的运行时错误(如预期的那样)。
请参阅下面的完整代码,我要缺少什么。
package com.example.tracker2;
import android.app.ActionBar;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
public class ListViewLoader extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {
SimpleCursorAdapter mAdapter;
String[] projection = {
BaseColumns._ID,
PriceListContract.PriceList.COLUMN1_NAME_TITLE,
PriceListContract.PriceList.COLUMN2_NAME_TITLE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
progressBar.setIndeterminate(true);
getListView().setEmptyView(progressBar);
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar);
String[] fromColumns = {PriceListContract.PriceList.COLUMN1_NAME_TITLE, PriceListContract.PriceList.COLUMN2_NAME_TITLE};
int[] toViews = {android.R.id.text1, android.R.id.text2}; // The TextView in simple_list_item_1
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter);
// !!This following line is the issue!!
getLoaderManager().initLoader(0, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, PriceListContract.PriceList.CONTENT_URI,
projection, null , null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}