0

hi im creating an app that displays data in custom listview inside the fragment. when i click the next button i got an error "Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference". My code is working in main activity but when i transfer it to fragment, i changes some code but i got error when clicking the next button

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

this is my error

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.jathniel.studentlist.FragmentAllStudents$2.onClick(FragmentAllStudents.java:94)

this is the xml layout

<?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>
4
  • 2
    Well. It's mean the in your XML file R.layout.allstudent there isn't ListView with id R.id.list. Check if you didn't put the wrong layout. Commented Oct 29, 2015 at 3:37
  • no sir i put the correct layout Commented Oct 29, 2015 at 4:19
  • please update the question with layout file. Commented Oct 29, 2015 at 4:39
  • posted already @RajanBhavsar Commented Oct 29, 2015 at 4:48

1 Answer 1

1

The problem is in your onClickListeners. you are using the local copy of view provided by the onClick(View view) function. So change your onClickListeners from onClick(View view) to 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()));

}

But now the lv.setAdapter(...) call becomes redundant. You are trying to do what you have done already. Perhaps you are trying to do something else and accidentally added this code instead. Otherwise, you should compact your onClick(...) to

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

}

Also you have to add the final modifier to the view object

final View view = inflater.inflate(R.layout.allstudent, container, false);
Sign up to request clarification or add additional context in comments.

6 Comments

when i apply your code which you remove the setadapter, the previous and next are not working
You have to add the final modifier to the view object. I've edited the answer. Well ,does the buttons work as expected if you don't remove those lines?
yes it works when its in main activity but when i transfer it to fragment, it doesnt work and forces close
when i insert the lv.setAdapter and instead of v, i used view in lv = (ListView) view.findViewById(R.id.list); it worked already
I couldn't understand what exactly you were trying to do. So it's working now. Good.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.