简体   繁体   中英

Demo ListFragment Error

I do a demo about ListFragment but it's error. What can i do? Who help me fix it. I study android a month so not good skill

ListFragment.Class

onCreateView{
    MenuAdapter adapter = new MenuAdapter(getActivity());
    menu = new Menu(R.drawable.ic_home, "Trang Chu");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_people, "Khuyen Mai");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_photos, "Cam Nang");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_communities, "Gan Toi");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_pages, "Video");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_whats_hot, "Gioi Thieu RedSun");
    adapter.add(menu);
    setListAdapter(adapter);
    return inflater.inflate(R.layout.list, null);
}

In my adapter,

ArrayAdapter<Menu> {

    Context context;

    public MenuAdapter(Context context) {
        super(context, 0);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(
                    R.layout.menulist_layout, null);
        }

        ImageView icon = (ImageView) view.findViewById(R.id.menuItem);
        icon.setImageResource(getItem(position).getImageRes());
        TextView title = (TextView) view.findViewById(R.id.menuText);
        title.setText(getItem(position).getTitle());

        return view;
    }

}

I don't known what error in this code.

The main issue is your passing 0 for resource id in custom adapter class.

You need to pass a valid resource ID of a layout file

 super(context, 0);

to

 super(context, android.R.layout.simple_list_item_1);  // or your own layout for the list item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM