android 中 fragment 嵌入listview

首先在MainActivity中

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

然后在主布局activity_main中只有一个fragment

        <fragment 
        android:id="@+id/title_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.news.TitleFragment"
        />

在com.example.news.TitleFragment里面
我定义了listview ,和自己定义的newsadapter适配器,
还有要装入listview中的数据data。

代码如下:

public class TitleFragment extends Fragment implements OnItemClickListener {
    private NewsAdapter adapter;
    private List<News> data;
    private ListView listView;
    private boolean isTwoPane;
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        adapter = new NewsAdapter(activity, R.layout.title_list_item, data);
        data = getNews();
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.i("test", "onCreateView");
        View view = inflater.inflate(R.layout.title_frag, container, false);
        listView = (ListView) view.findViewById(R.id.listView1);
        listView.setAdapter(adapter);
        // listView.setOnItemClickListener(this);
        return view;
    }

    private List<News> getNews() {
        List<News> data = new ArrayList<News>();
        News news1 = new News();
        news1.setTitle("chongqing is beautiful city!!!");
        news1.setContext("There have everything in ChongQing,delicious food ,great building and hot girls ");
        data.add(news1);

        News news2 = new News();
        news2.setTitle("chongqing is beautiful city2222!!!");
        news2.setContext("There have everything in ChongQing,delicious food ,great building and hot girls ");
        data.add(news2);
        return data;

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub

    }
}

在newsadapter适配器:
我试过不用自己定义的适配器就可以,但是用自己定义的适配器就会出错,但是我不知道到底错误在哪。

代码如下:

 public class NewsAdapter extends ArrayAdapter<News> {
    private int resourceId;
    public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        News news = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

        } else {
            view = convertView;
        }
        TextView titleText = (TextView) view.findViewById(R.id.title);
        titleText.setText(news.getTitle());
        return view;
    }
}

R.layout.title_list_item代码:

 <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>

最后就出现binary xml file line #11:error inflating class fragment 错误。

本人安卓小白一名,求大神告诉我到底哪里错了呀。跪谢

2个回答

不明白这里为什么这样写 LayoutInflater.from(getContext()).inflate(resourceId, null);
一般是LayoutInflater.from(getContext()).inflate(R.layut.xxx, null);

而这个报错binary xml file line #11,说的是xml文件第11行有问题,请核对,或者把xml文件共享看下

sinat_35054086
0kangkang0 首先谢谢你的回复,我是通过newsadapter的构造函数将R.layout.……传入并且赋值给resourceId,那个xml11行其实就是主布局文件xml <fragment 就就是第11行。 ……… </fragment>
3 年多之前 回复

你的问题解决了么?没解决可以吱个声。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问