Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - "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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...