qq_37761523
2017-03-15 07:55
采纳率: 57.1%
浏览 2.4k

android fragment中listview.setAdapter为空的问题

android新手想多联系下fragment的使用,可在listview.setAdapter时遇到了如下问题:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.sms.InsertFragment.onCreate(InsertFragment.java:73)
以下是代码

  private ListView insert_choose_listView;
    ArrayList<HashMap<String, String>> listItem;
    MyAdapter myAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        listItem = new ArrayList<HashMap<String,String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("Insert"," Insert Teacher");listItem.add(map);
        map.put("Insert"," Insert Student");listItem.add(map);
        map.put("Insert"," Insert Course");listItem.add(map);

        myAdapter=new MyAdapter(this.getActivity());
        insert_choose_listView.setAdapter(myAdapter);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_insert, container, false);
        insert_choose_listView=(ListView)view.findViewById(R.id.insert_choose_listview);

        return view;
    }

    private class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局

        public MyAdapter (Context context){
            this.mInflater= LayoutInflater.from(context);
        }
        public int getCount(){
            return listItem.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            InsertFragment.MyAdapter.ViewHolder holder;
            //观察convertView随ListView滚动情况
            Log.v("MyListViewBase", "getView " + position + " " + convertView);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.flistviewlayout,null);
                holder = new InsertFragment.MyAdapter.ViewHolder();
                /**得到各个控件的对象*/
                holder.title = (TextView) convertView.findViewById(R.id.ftext);
                convertView.setTag(holder);//绑定ViewHolder对象
            }
            else{
                holder = (InsertFragment.MyAdapter.ViewHolder)convertView.getTag();//取出ViewHolder对象
            }
            /**设置TextView显示的内容,即我们存放在动态数组中的数据*/
            holder.title.setText(listItem.get(position).get("Insert").toString());

            return convertView;
        }


        /**存放控件*/
        public final class ViewHolder{
            public TextView title;
        }

报错的73行是insert_choose_listView.setAdapter(myAdapter);这句

我在Activity中这样加入ListView是不会报错的可是放到Fragment中就成了空的
求解答

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Juice_Lover 2017-03-15 08:12
    已采纳

    重写onViewCreate()方法 在里面findViewById
    insert_choose_listView=(ListView)view.findViewById(R.id.insert_choose_listview);

    已采纳该答案
    打赏 评论
  • Juice_Lover 2017-03-15 08:28

    onCreate→onCreateView→onViewCreate→onActivityCreate

    打赏 评论
  • Mr康 2017-03-15 09:17

    你把 myAdapter=new MyAdapter(this.getActivity());
    insert_choose_listView.setAdapter(myAdapter);放到 insert_choose_listView=(ListView)view.findViewById(R.id.insert_choose_listview);下面

    打赏 评论
  • 鼎鼎浩 2017-03-16 11:52

    你就只声明了Listview的控件没有绑定id,他当然找不到了,onCreate()方法后边要绑定id ----findViewById()

    打赏 评论

相关推荐 更多相似问题