I'm creating an app in android studio in Java and I'm trying to return the size of a List using the .size() function but the app on my device keeps crashing when I use it.
To see my result on the app I've initialized a TextView
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
TextView testTextView = findViewById(R.id.testText);
List<String> list = Arrays.asList("test","try");
testTextView.setText(list.size());
I've tried to use other functions to see if I have wrote correctly the List "list" or not, and when I used :
testTextView.setText(list.get(0));
It worked and shows test
as a result
I do not think it is a null pointer exception because we can access the List with the .get() function. But if it is do you have any clue why ?
It seems the problem come form this line:
testTextView.setText(list.size());
the text view take strings as parameters but you give them an Int, causing your problem. In order to fix that you can Use the String.valueOf() function in java to convert the integer size to a string that would be accepted by the component:
testTextView.setText(String.valueOf(list.size()));