Android Fragment:尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

时间:2015-10-29 03:23:41

标签: android android-fragments android-studio android-adapter

我正在创建一个应用程序,在片段内的自定义列表视图中显示数据。当我点击下一个按钮时,我得到一个错误“尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'”。我的代码在主要活动中工作但是当我将它转移到片段时,我更改了一些代码,但是当我点击下一个按钮时出现错误

public class FragmentAllStudents extends Fragment {

        List<Students> GetAll;
        DatabaseHelper db = new DatabaseHelper(getActivity());
        ListView lv;
        Context context = getActivity();
        DatabaseHelper dbhelper;
        Button btnprevious,btnnext;

        int index = 0;
        private int currentPageIndex = 0;


        public FragmentAllStudents(){

        }

        @Override
        public void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

                View view = inflater.inflate(R.layout.allstudent, container, false);

                dbhelper = new DatabaseHelper(getActivity());
                //Add below lines to your original code
                try{
                        dbhelper.createDataBase();
                }
                catch(IOException e){
                        e.printStackTrace();
                }
                try {
                        dbhelper.openDataBase();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                GetAll = dbhelper.getAll(index);
                lv = (ListView) view.findViewById(R.id.list);
                lv.setAdapter(new ViewAdapter(getActivity()));

                btnprevious = (Button) view.findViewById(R.id.button1);
                btnnext = (Button) view.findViewById(R.id.button2);

                btnprevious.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                                currentPageIndex -= 20;
                                GetAll = dbhelper.getAll(currentPageIndex);
                                lv = (ListView) view.findViewById(R.id.list);
                                lv.setAdapter(new ViewAdapter(getActivity()));

                        }

                });

                btnnext.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {

                                currentPageIndex += 20;
                                GetAll = dbhelper.getAll(currentPageIndex);
                                lv = (ListView) view.findViewById(R.id.list);
                                lv.setAdapter(new ViewAdapter(getActivity()));

                        }
                });

                return view;
        }

        private class ViewAdapter extends BaseAdapter {

                LayoutInflater mInflater;

                public ViewAdapter(Activity activity) {
                        mInflater = LayoutInflater.from(activity);
                }

                @Override
                public int getCount() {
                        return GetAll.size();
                }

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

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

                @Override
                public View getView(final int position, View convertView, ViewGroup parent) {

                        if (convertView == null) {
                                convertView = mInflater.inflate(R.layout.list_item,null);
                        }

                        final TextView names = (TextView) convertView.findViewById(R.id.studentlist_name);
                        final TextView gender = (TextView) convertView.findViewById(R.id.studentlist_gender);

                        names.setText(GetAll.get(position).getname());
                        gender.setText(GetAll.get(position).getgender());

                        return convertView;
                }
        }
}

这是我的错误

  

尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'               在com.example.jathniel.studentlist.FragmentAllStudents $ 2.onClick(FragmentAllStudents.java:94)

这是xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Back"
            android:id="@+id/button1"
            android:layout_gravity="center_horizontal" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:id="@+id/button2" />

    </LinearLayout>


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="0.5dp"
        android:drawSelectorOnTop="true"
        android:footerDividersEnabled="false"
        android:padding="10dp"
        android:scrollbarStyle="outsideOverlay" >

    </ListView>


</LinearLayout>

1 个答案:

答案 0 :(得分:1)

问题出在您的onClickListener中。您正在使用view函数提供的onClick(View view)的本地副本。因此,将onClickListeners从onClick(View view)更改为onClick(View v)

@Override
public void onClick(View v) {
     currentPageIndex -= 20;
     GetAll = dbhelper.getAll(currentPageIndex);
     lv = (ListView) view.findViewById(R.id.list);
     lv.setAdapter(new ViewAdapter(getActivity()));

}

但现在lv.setAdapter(...)电话变得多余了。你正在尝试做你已经做过的事情。也许您正在尝试做其他事情而不小心添加了此代码。否则,您应该将onClick(...)缩小为

@Override
public void onClick(View v) {
     currentPageIndex -= 20;
     GetAll = dbhelper.getAll(currentPageIndex);

}

此外,您必须将最终修饰符添加到view对象

final View view = inflater.inflate(R.layout.allstudent, container, false);