问题来了。我有一个 ListView ,它在建筑物中看起来很好,但它打破了错误。 内容具有 id 属性为“android.r.id.list”的 View ,该 View 不是 ListView 类。
我还没有将 ListView 添加到 XML 中,比如 :@android:list,我没有这样做的原因是我可以找到一些示例,它们不需要创建这样的 xml 列表,所以应该怎么办我现在做的?
如何解决?谢谢!
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 中。 虽然 image/name/sex 已经显示,但它们不在列表中,似乎列表是一个单独的元素。有什么想法可以让我将图像/姓名/性别放入列表布局中吗?谢谢!
最佳答案
I haven't added the listview into the XML
添加一个 ListView
和 @android:id/list
的 android:id
到你的布局,你希望列表出现的地方.
或者,删除您的onCreateView()
实现,这样您就可以获得从ListFragment
继承的ListView
。
或者,将您的 fragment 更改为继承自Fragment
,而不是ListFragment
,并自己管理一个ListView
。
或者,将您的 fragment 更改为继承自 Fragment
,并且不要尝试在其中显示列表。
关于android - "Content has view with id attribute ' android.r.id.list ' that is not a ListView Class."在 fragment 中创建 ListView 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22112966/