qq_37761523 2017-03-15 07:55 采纳率: 57.1%
浏览 2733
已采纳

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()

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#abap#的问题,请各位专家解答!
  • ¥20 内网通过公网访问外网问题
  • ¥15 怎么使请求通过cors
  • ¥15 WDM 驱动ACPI 相关疑问
  • ¥15 prism 跨窗体共享数据绑定 wpf
  • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
  • ¥15 0基础计算机毕设,应该从哪开始?
  • ¥60 使用DKT40脑图划分ROI区域
  • ¥15 有偿解决C51单片机液晶屏12864显示问题
  • ¥15 IDEA构建失败?怎么搞