Reputation: 1714
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>
Upvotes: 0
Views: 2013
Reputation: 502
The problem is in your onClickListener
s. 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);
Upvotes: 1