Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Content has view with id attribute 'android.r.id.list' that is not a ListView Class." when create a list view in a fragment

Here is the problem. I have a list view, and it looks fine in the building, but it broke the error. Content has view with id attribute 'android.r.id.list' that is not a ListView Class.

I haven't added the listview into the XML, something like :@android:list, the reason I didn't do that is I could find some examples which they no need to create such of xml list, so what should i do now?

How to fix it? thanks!

Code for the Fragment.java

package com.example.demo3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ContactsFragment extends ListFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View contactLayout = inflater.inflate(R.layout.contacts_layout,
                container, false);
        return contactLayout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Activity currentActivity = getActivity();

        SimpleAdapter adapter = new SimpleAdapter(currentActivity, getData(),
                R.layout.contacts_layout,
                new String[] { "title", "info", "img" }, new int[] {
                        R.id.friend_name, R.id.friend_sex, R.id.friend_img });

        setListAdapter(adapter);

    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "G1");
        map.put("info", "google 1");
        map.put("img", R.drawable.background_login);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "G2");
        map.put("info", "google 2");
        map.put("img", R.drawable.background_login);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "G3");
        map.put("info", "google 3");
        map.put("img", R.drawable.background_login);
        list.add(map);

        return list;
    }

    private TextView findViewById(int testmessage) {
        // TODO Auto-generated method stub
        return null;
    }

}

code for XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/friend_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/friend_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="22px" />

        <TextView
            android:id="@+id/friend_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="13px" />
    </LinearLayout>

</LinearLayout>

updated: I have added the

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

into the XML. Although the image/name/sex has shown, but they are not inside the list, seems the list is a separate element. Any idea to let me to put the image/name/sex into the list layout? THXXX!

like image 990
Tianbing Leng Avatar asked Mar 01 '14 10:03

Tianbing Leng


3 Answers

I was using:

android:id="@+id/listView"

Then, I have replaced to it:

android:id="@android:id/list"
like image 79
Dheeraj D Avatar answered Nov 11 '22 07:11

Dheeraj D


You can inflate your View on onCreateView() method in which layout your ListView and id of that ListView must be

@android:id/list

and after that on onCreateView() method

ListView lv = (ListView)contactLayout.findViewById(android.R.id.list);
like image 36
Piyush Avatar answered Nov 11 '22 07:11

Piyush


I haven't added the listview into the XML

Add a ListView with an android:id of @android:id/list to your layout, where you want the list to appear.

Or, delete your onCreateView() implementation, so you get the inherited ListView that you get from ListFragment.

Or, change your fragment to inherit from Fragment, not ListFragment and manage a ListView by yourself.

Or, change your fragment to inherit from Fragment and do not attempt to show a list in it.

like image 42
CommonsWare Avatar answered Nov 11 '22 08:11

CommonsWare