我正在努力编译和运行来自 Android 开发者网站的示例:http: //developer.android.com/guide/topics/ui/layout/listview.html
这是我输入的版本:
package com.chex.control;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
public class ListViewExample extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
Cursor cursor;
// database columns that we will retreive
final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME };
final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
+ " NOTNULL AND (" + ContactsContract.Data.DISPLAY_NAME
+ " != ''))";
SimpleCursorAdapter adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] fromColumns = { ContactsContract.Data.DISPLAY_NAME };
int[] toViews = { android.R.id.text1 };
ListAdapter adapter = new SimpleCursorAdapter(this, // context
android.R.layout.simple_list_item_1, cursor, // cursor to bind
// to
fromColumns, // array of cursor
// columns to
// bind to
toViews, 0); // parallel
// array
// of
// which
// template
// objects
// to
// bind
// to
// cursor
// columns
setListAdapter(adapter);
// ******* THE FOLLOWING LINE WON'T COMPILE **************
getLoaderManager().initLoader(0, null, this);
}
@Override
public void setListAdapter(ListAdapter adapter) {
// TODO Auto-generated method stub
super.setListAdapter(adapter);
}
/**
* create and return a CursorLoader that will take care of creating a Curso
* for the data being displayed.
*/
@Override
public Loader onCreateLoader(int arg0, Bundle arg1) {
CursorLoader loader = new CursorLoader(this,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
Eclipse 给出编译错误:
The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, ListViewExample).
显然,演员不想这样是不是例子错了?如果我只是从 Android 开发者网站剪切和粘贴,我也会遇到同样的问题。
当我在代码中更改<Cursor>
为时<D>
,强制转换在执行过程中失败。我不知道为什么——在我看来,班级履行了LoaderCallbacks
合同implements <D>