findViewById returns a null object reference in a listView
Hi everyone I started coding in java since 2 days and now I'm facing a problem, I found topics that were talking about this, but no one has provided me the right solution.
Im trying to create a "checklist using listView and android BasicAdapter, so I got my row layout xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/procedure_title_text_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="@color/black"
android:text="@string/procedure_title_default_name"
android:layout_marginStart="30dp"
android:layout_marginTop="15dp"/>
<CheckBox
android:id="@+id/procedure_title_checkBox"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/procedure_title_text_View"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
When I try to add this row layout to my list view everything works fine but when I try to change the paramaters of this row layout, like the text in the TextView using this code (someone told me to use a ViewHolder but it doesnt changed anything) :
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.adapter_procedure, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.procedure_title_text_View);
holder.title_checked = (CheckBox) convertView.findViewById(R.id.procedure_title_checkBox);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
ProcedureTitle currentTitle = getItem(position);
String titleName = currentTitle.getTitle();
boolean titleChecked = currentTitle.getChecked();
holder.title.setText(titleName);
holder.title_checked.setChecked(titleChecked);
return convertView;
}
private static class ViewHolder {
TextView title;
CheckBox title_checked;
}
}
The application just crashes and gives me an error like this:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.protostage2_v1.adapter.ProcedureTitleAdapter.getView(ProcedureTitleAdapter.java:75)
So I tried to run it with the debug mode, and I found that the line (below), returns a null result. The TextView is then not located and causes the crash of the application.
holder.title = (TextView) convertView.findViewById(R.id.procedure_title_text_View);
I'm looking to try to fix this, if you have any idea on how to solve it, I would appreciate your help ! :)
Answers 1
I solved my problem oh my god how could i be that stupid, sorry.