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

Android Fragment: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

你好,我正在创建一个在片段内的自定义列表视图中显示数据的应用程序。当我单击下一步按钮时,出现错误 "Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference"。我的代码在 main activity 中工作,但是当我将它转移到 fragment 时,我更改了一些代码但是在单击下一步按钮时出现错误

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;
                }
        }
}

这是我的错误

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.jathniel.studentlist.FragmentAllStudents.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>

问题出在您的 onClickListener 上。您正在使用 onClick(View 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);