I am guessing my problem is with inflating layouts. I have 2 layout files:
activity_mail.xlm:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/profilesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="18sp" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
... and listitem_device.xlm:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/device_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"/>
<TextView
android:id="@+id/device_service"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"/>
</LinearLayout>
in my main_activity java file, I have:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_label); // from listitem_device.xml
viewHolder.profilesCount = (TextView) view.findViewById(R.id.profilesCount); // from activity_main.xml
The problem is the subsequent code:
viewHolder.profilesCount.setText("HELLO");
generates the error "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference".
How do I fix this?
I don't see anywhere that you have even have a view with id "profilesCount".You only have device label and device service as ids in the layout file that you are using to inflate the rows of the list view.Better use Recycler view also, it provides more flexibility.
Also, you can't access something from activity_mail.xml in the getView() function of your adapter because by using "view" in getfunction, as that corrspond to only the row of the list view and since that id is not present in the xml file associated with the row of list view, you are getting "viewholders.profileCount" as null and hence the Nullpointer exception.
If you wanna use a TextView or any view in your adapter, better create an interface and implement that interface in your activity to use as a call back and then set the textview field of your activity.xml file, thats the best way as it would keep your code cleaner.Other way is to pass the reference of the activity to your adapter and then find the text view or whatever that you wanna use from actvity_mail.xml file, but its not recommended and is a very bad way as it may require additional memory.