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文件共享看下

    评论

报告相同问题?

悬赏问题

  • ¥100 高价邀请复制 域天d8联网狗
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?