问题描述
这是问题所在。我有一个列表视图,它看起来很好的建筑,但它打破了错误。内容有鉴于与id属性android.r.id.list这不是一个ListView类。
我还没有添加列表视图到XML,是这样的:@android:列表中,我并没有那么做是我能找到一些例子的原因,他们没有必要建立这样的XML列表,那么又该我现在做什么?
如何解决?谢谢!
code为Fragment.java
包com.example.demo3;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口的java.util.Map;
进口android.app.Activity;
进口android.app.ListFragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;
公共类ContactsFragment扩展ListFragment {
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看contactLayout = inflater.inflate(R.layout.contacts_layout,
集装箱,假);
返回contactLayout;
}
@覆盖
公共无效onActivityCreated(包savedInstanceState){
super.onActivityCreated(savedInstanceState);
活动currentActivity = getActivity();
SimpleAdapter适配器=新SimpleAdapter(currentActivity,getData()会,
R.layout.contacts_layout,
新的String [] {称号,信息,IMG},新的INT [] {
R.id.friend_name,R.id.friend_sex,R.id.friend_img});
setListAdapter(适配器);
}
私人名单,其中,地图<字符串,对象>>的getData(){
列表与LT;地图<字符串,对象>>名单=新的ArrayList<地图<字符串,对象>>();
地图<字符串,对象>图=新的HashMap<字符串,对象>();
map.put(标题,G1);
map.put(信息,谷歌1);
map.put(IMG,R.drawable.background_login);
list.add(图)
图=新的HashMap<字符串,对象>();
map.put(标题,G2);
map.put(信息,谷歌2);
map.put(IMG,R.drawable.background_login);
list.add(图)
图=新的HashMap<字符串,对象>();
map.put(标题,G3);
map.put(信息,谷歌3);
map.put(IMG,R.drawable.background_login);
list.add(图)
返回列表;
}
私人TextView的findViewById(INT的TestMessage){
// TODO自动生成方法存根
返回null;
}
}
code为XML:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
< ImageView的
机器人:ID =@ + ID / friend_img
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5像素/>
<的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / FRIEND_NAME
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#000000
机器人:TEXTSIZE =22px/>
<的TextView
机器人:ID =@ + ID / friend_sex
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#000000
机器人:TEXTSIZE =13px的/>
< / LinearLayout中>
< / LinearLayout中>
更新:我已经添加了
<的ListView
机器人:ID =@机器人:ID /列表
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1>
< / ListView控件>
到XML。虽然图像/名称/性别已经示出,但它们不是列表内,似乎该列表是一个单独的元件。任何想法让我把图像/名/性别到列表中的布局? THXXX!
添加的ListView
与安卓的ID
@android:ID /名单
来布局,要出现在列表中。
或者删除 onCreateView()
的实施,让您得到继承的ListView
你从获得 ListFragment
。
或者改变你的片段从片段
,不是 ListFragment
继承和管理 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.
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!
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.
这篇关于&QUOT;内容有鉴于与id属性“android.r.id.list”这不是一个ListView类&QUOT。当在一个片段创建列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!