1

i can manage to make a listfragment with same icon. but it seems i cant make that list with different icon. so the list result will be different icon for different item list. i try with it. and when i try to build it. it shows no error. but when i run it. its just force closes. here is my logcat.

04-03 16:49:13.513: W/dalvikvm(9236): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-03 16:49:13.602: E/AndroidRuntime(9236): FATAL EXCEPTION: main
04-03 16:49:13.602: E/AndroidRuntime(9236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.surat.pendek/com.surat.pendek.utama}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.os.Looper.loop(Looper.java:137)
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-03 16:49:13.602: E/AndroidRuntime(9236): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list''

here is my sample fragment that extends listfragment, this fragment contain adapter too :

public class SampleListFragment extends ListFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SampleAdapter adapter = new SampleAdapter(getActivity(), null);
    for (int i = 0; i < 20; i++) {
        adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
    }
    setListAdapter(adapter);
}

private class SampleItem {
    public String tag;
    public int iconRes;
    public SampleItem(String tag, int iconRes) {
        this.tag = tag; 
        this.iconRes = iconRes;
    }
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

    private final Context context;
      private final String[] values;


    public SampleAdapter(Context context, String[] values) {
        super(context, R.layout.row);
        this.context = context;
        this.values = values;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
        }
        ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
        icon.setImageResource(getItem(position).iconRes);
        TextView title = (TextView) convertView.findViewById(R.id.row_title);
        title.setText(getItem(position).tag);

         String surat = title.getText().toString();
            if (surat.equalsIgnoreCase("Huruf Hijaiyah")) {
              icon.setImageResource(R.drawable.aien);
            } else
                if (surat.equalsIgnoreCase("About")) {
              icon.setImageResource(R.drawable.alief);
            } else
                if (surat.equalsIgnoreCase("Surat+arti")) {
                      icon.setImageResource(R.drawable.ba);
                    } else
                        if (surat.equalsIgnoreCase("Surat+latin")) {
                              icon.setImageResource(R.drawable.emo_im_angel);
                            } else
                                {
                                      icon.setImageResource(R.drawable.emo_im_happy);
                                    }



        return convertView;
    }

}

}

and here is my fragment that extends listfragment :

public class MenuFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] surat = getResources().getStringArray(R.array.surat_pendek);
    ArrayAdapter<String> sampleAdapter = new ArrayAdapter<String>(getActivity(), 
            R.layout.row, R.id.row_title, surat);
    setListAdapter(sampleAdapter);
}
4

1 回答 1

1

有点晚了,但我希望这个答案有所帮助。

正如 logcat 所说:Your content must have a ListView whose id attribute is 'android.R.id.list'.

您似乎有一个明确的 xml 布局文件:R.layout.list. 这个布局应该定义一个带有 id 的列表android.R.id.list

于 2013-12-16T15:56:48.333 回答