& quot;内容具有ID属性为' android.r.id.list'的视图那不是ListView类.在片段中创建列表视图时 [英] "Content has view with id attribute 'android.r.id.list' that is not a ListView Class." when create a list view in a fragment

查看:49
本文介绍了& quot;内容具有ID属性为' android.r.id.list'的视图那不是ListView类.在片段中创建列表视图时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题所在.我有一个列表视图,在建筑物中看起来不错,但它打破了错误.内容具有ID属性为"android.r.id.list"的视图,该视图不是ListView类.

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.

我还没有将listview添加到XML中,例如:@android:list,我没有这样做的原因是我可以找到一些他们不需要创建xml列表的示例,那么应该怎么做呢?我现在做的?

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?

如何解决?谢谢!

Fragment.java的代码

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;
    }

}

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>

已更新:我已经添加了

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

转换为XML.尽管已显示图像/名称/性别,但它们不在列表中,似乎列表是一个单独的元素.有什么想法让我将图像/名称/性别放入列表布局吗?THXXX!

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!

推荐答案

我还没有将列表视图添加到XML中

I haven't added the listview into the XML

在布局中添加一个 ListView ,其中 android:id @android:id/list

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

或者,删除您的 onCreateView()实现,以获取从 ListFragment 获得的继承的 ListView .

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

或者,将您的片段更改为继承自 Fragment ,而不是 ListFragment ,并自行管理 ListView .

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

或者,将您的片段更改为从 Fragment 继承,并且不要尝试在其中显示列表.

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

这篇关于&amp; quot;内容具有ID属性为&amp;#39; android.r.id.list&amp;#39;的视图那不是ListView类.在片段中创建列表视图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆