问题
So I'm getting 3 errors "final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);" & "public class MyAdapter extends CursorAdapter implements SectionIndexer" <---this one is getting marked as an error twice.
My MainActivityNext.java
package testing.android.application.three;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.widget.AlphabetIndexer;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
public class MainActivityNext extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_next);
}
public class MyAdapter extends CursorAdapter implements SectionIndexer
{
// All valid characters. May want to include numbers, etc if they show up
// in your sort column
private final static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
private AlphabetIndexer mIndexer = null;
public MyAdapter(Context context, Cursor c, int flags)
{
super(context, c, flags);
// Assumes your cursor is non-null,
// otherwise do this in swapCursor if mIndexer==null
final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);
mIndexer = new AlphabetIndexer(c, sortColumnIndex,
ALPHABET);
}
public Cursor swapCursor(Cursor newCursor)
{
super.swapCursor(newCursor);
// Make sure the AlphabetIndexer knows about the new Cursor
mIndexer.setCursor(newCursor);
return newCursor;
}
public int getPositionForSection(int section)
{
// AlphabetIndexer does all the hard work
return mIndexer.getPositionForSection(section);
}
public int getSectionForPosition(int position)
{
// AlphabetIndexer does all the hard work
return mIndexer.getSectionForPosition(position);
}
public Object[] getSections()
{
// AlphabetIndexer does all the hard work
return mIndexer.getSections();
}
}
}
回答1:
According to your Logcat:
Unable to start activity ComponentInfo{testing.android.application.three/testing.android.application.three.MainActivityNext}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Whenever you use or extend a listactivity the view you build for it must have a listview with that id.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
See http://developer.android.com/reference/android/app/ListActivity.html for more info on it.
回答2:
as in log:
our content must have a ListView whose id attribute is 'android.R.id.list'
means you will need to use @id/android:list
id in xml for ListView. do it as:
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
..../>
回答3:
If your activity extends the ListActivity then you have to set the id of the xml of your ListView like below
< ListView
android:id="@android:id/list"
...
来源:https://stackoverflow.com/questions/16884897/eclipse-android-error