我需要你的帮助我尝试解决这个问题,因为5天。我尝试在我的视图parger上使用自定义视图的ListFragment。但是当ListFragment的选项卡处于活动状态时,我收到错误。我说法语所以如果我有错误请。 这是我的错误
08-01 22:46:13.387: E/AndroidRuntime(8452): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
这是我的listFragment类:
public class MomentFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceBundle){
List<Integer> list = new ArrayList<Integer>();
list.add(R.array.pref_light_color);
CustomMomentListViewAdapter aa = new CustomMomentListViewAdapter(getActivity());
setListAdapter(aa);
return inflater.inflate(R.layout.fragment_core_moment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
public static MomentFragment newInstance(){
MomentFragment fragment = new MomentFragment();
return fragment;
}
}
这是我的customAdapter类
public class CustomMomentListViewAdapter extends BaseAdapter{
private List<Integer> mModel = new ArrayList<Integer>();
private Context mContext;
public CustomMomentListViewAdapter(Context context){
mContext = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mModel.size();
}
@Override
public Integer getItem(int position) {
// TODO Auto-generated method stub
return mModel.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomMomentListView v = null;
/* if view doesn't exist we create it */
if(convertView == null){
v = new CustomMomentListView(mContext);
}else{
v = (CustomMomentListView) convertView;
}
v.bind(getItem(position));
return v;
}
public void bind(List<Integer> model){
mModel = model;
}
}
这是我的自定义列表视图(custom_moment_list_view.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/dialog_holo_light_frame"
android:orientation="vertical" >
<TextView
android:id="@+id/moment_item_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="@string/moment_empty" />
</LinearLayout>
最后在listFragment(fragment_core_moment.xml)的onCreate函数中使用的视图源
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/moment_empty" />
</RelativeLayout>
感谢您的帮助。
答案 0 :(得分:0)
将此代码移至onCreteView()
onActivityCreated()
方法
List<Integer> list = new ArrayList<Integer>();
list.add(R.string.text);
CustomMomentListViewAdapter aa = new CustomMomentListViewAdapter(getActivity());
aa.bind(list);
setListAdapter(aa);