I've created my own custom adapter for a ListView that is in a fragment. I believe I have everything correct, but it still throws an error when launching my app and it crashes. I tried to debug it, but ended up not being able to locate the issue.
Here's my code for the fragment:
public class TabFragment0 extends Fragment {
Button addPR;
ArrayList<PRObject> PRList;
PRObjectAdapter PRListAdapter;
private ListView listViewer;
private final String PRKey = "PRArgs";
public TabFragment0() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab_personalrecords, container,
false);
if(loadList(PRKey) != null) {
PRList = loadList(PRKey);
}
PRListAdapter = new PRObjectAdapter(getActivity(), PRList);
listViewer = (ListView) view.findViewById(R.id.prListView);
listViewer.setAdapter(PRListAdapter);
...................................................
The line listViewer.setAdapter(PRListAdapter);
is what throws the error. I've also tried PRListAdapter = new PRObjectAdapter(this.getActivity(), PRList);
as wel as PRListAdapter = new PRObjectAdapter(getContext(), PRList);
to no avail.
Here's my code for the adapter class:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class PRObjectAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<PRObject> objects;
private class ViewHolder {
TextView textview1;
TextView textview2;
}
public PRObjectAdapter(Context context, ArrayList<PRObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PRObjectAdapter.ViewHolder holder = null;
if(convertView == null) {
holder = new PRObjectAdapter.ViewHolder();
convertView = inflater.inflate(R.layout.pr_item_layout, null);
holder.textview1 = (TextView)
convertView.findViewById(R.id.prTitleTV);
holder.textview2 = (TextView)
convertView.findViewById(R.id.prNumTV);
convertView.setTag(holder);
} else {
holder = (PRObjectAdapter.ViewHolder) convertView.getTag();
}
holder.textview1.setText(objects.get(position).getTitle());
holder.textview2.setText(objects.get(position).getNum());
return convertView;
}
}
I want to say that the issue is in my View getView
method, but I can't pinpoint the issue.
Any help would be greatly appreciated.
EDIT:
This question is not a duplicate of this NullPointerException question
My question specifically pertains to the fragment/customAdapter combination in Android Studio, whereas the above link is just a guide on what a NPE is and how to fix it.