0kangkang0 2016-10-17 11:39 采纳率: 0%
浏览 1348

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条回答

  • 徐福记456 2016-10-17 13:34
    关注

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog