qq_37761523
qq_37761523
2017-03-15 07:55

android fragment中listview.setAdapter为空的问题

  • 控件
  • android
  • arraylist

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