我正在尝试将ListFragment添加到Gingerbread中的Activity,但是当创建Activity并且没有堆栈跟踪时Android崩溃。我已经验证我正在使用兼容库导入。这是我的ListFragment代码:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class ListViewFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
List listData = getListData().
setListAdapter(new ListViewAdapter(getActivity(), listData));
}
class ListViewAdapter extends BaseAdapter implements ListAdapter {
.
.
.
}
}
我也尝试过:
ListView listView = getListView();
listView.setAdapter(new ListViewAdapter(getActivity(), listData);
代替setListAdapter()调用,结果相同。
当我从Fragment扩展并使用ListView扩展布局文件时,它可以正常工作。当我尝试在ListFragment中使用固有的ListView时似乎有些不同。
非常感谢任何见解。
答案 0 :(得分:0)
在onCreateView上尝试比onActivityCreated更好。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = super.onCreateView( inflater, container, savedInstanceState );
List listData = getListData().
setListAdapter(new ListViewAdapter(getActivity(), listData));
return v;
}//met
答案 1 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_layout, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Set up your list in here
}