0

I have a problem which I cannot understand how to solve. I have a movie_item.xml which contains a textView and button as component. my custom ArrayAdapter gets ArrayLists which contain 10 items inside, which means my ArrayAdapter should draw the layout 10 times in my ListView. Problem is my application textView becomes null when ArrayAdapter works with the layout second time.

Here are my codes:

CustomArrayAdapter.java

package com.example.labb4;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomArrayAdapter extends ArrayAdapter {
ArrayList<Integer> movieIDList;
public CustomArrayAdapter(Context context, ArrayList titleList, ArrayList idList) {
    super(context, 0, titleList);
    movieIDList = idList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Om vyn används första gången
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_item, parent, false);
    }
    String title = getItem(position).toString();
    int movieID = movieIDList.get(position);
    TextView text = (TextView) convertView.findViewById(R.id.TitleTextView);
    Log.d("movies","text: "+text+"");
    text.setText(title);
    text.setId(movieID);
    Button button = (Button) convertView.findViewById(R.id.button);
    Log.d("movies","button "+button+"");
    button.setText("Lägg till");
    //Returnera hela Vy
    return convertView;
 }
}

this is movie_item.xml in res folder

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/TitleTextView"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="TextView" />

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>

as you see, I use inflate() to work with layout file, and the layout file already contains all components what I need for my job. But, when I run the program the output is

enter image description here

Boom, the textView becomes null on second time. My app crashes because setText on null is impossible. But the most mysterious part is if I remove the textView codes from getView on CustomArrayAdapter and print out only Buttons,

   public View getView(int position, View convertView, ViewGroup parent) {
    //Om vyn används första gången
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_item, parent, false);
    }
    String title = getItem(position).toString();
    int movieID = movieIDList.get(position);
    Button button = (Button) convertView.findViewById(R.id.button);
    Log.d("movies"," button "+button);
    button.setText("Lägg till");
    //Returnera hela Vy
    return convertView;
}

enter image description here Button is never null!

I just cannot understand what is making TextView null??

My application works if I make an if statement which checks if test is null or not,

     TextView text = (TextView) convertView.findViewById(R.id.TitleTextView);
    if(text!=null){
        Log.d("movies","text: "+text+"");
        text.setText(title);
        text.setId(movieID);
    }

But I don't think this is a right solution, for me it feels like.. solve the effect, not the reason of the problem..because Log.d shows TextView is null more than 10 times ..

enter image description here

So... why is my TextView is null? How can I solve this? I already have tried clean and rebuild projects. Restarting android studio didn't help me either.

game lover
  • 114
  • 2
  • 7
  • 1
    `text.setId(movieID);` – There's your problem. I'm not sure why you're setting the ID there, or if that's what you actually meant to do, but once you set that ID to something else, `convertView.findViewById(R.id.TitleTextView)` is going to return null when that `View` is recycled. Did you maybe mean to set the tag instead? – Mike M. Oct 30 '21 at 00:16
  • 1
    @MikeM. Holly shit!!! I think you are right. You are my lifesaver! I added setId there because I wanted to add clickEvent to the textView and send the id to another activity using intent... – game lover Oct 30 '21 at 00:19

0 Answers0