Print ArrayList in TextView

1

Hi, I'm doing a game and I need at the end of the game to show me the last 10 scores, so I create an arraylist where I'm going to store the scores. The problem is when I want to show the scores in a textview. I can not do it. application

ArrayList<String> list = new ArrayList<>(9);

    public void resultado(){
    if (win) {
        result.setBackgroundResource(R.color.verde);
        result.setText("GANASTE!");
        result.setVisibility(View.VISIBLE);
        puntaje=String.valueOf("Ganaste en la jugada "+puntajefinal);
        list.add(0,puntaje);
        mostrarrlista();
    } else {
        result.setBackgroundResource(R.color.rojo);
        result.setText("PERDISTE");
        result.setVisibility(View.VISIBLE);
        puntaje=String.valueOf("Perdiste en la jugada "+puntajefinal);
        list.add(0,puntaje);
        mostrarrlista();
    }
}
public void mostrarrlista(){
    for (int i=0;i<=9;i++){
        txt0.setText(list.get(i));
    }
}
  

05-21 21: 31: 52.831 1168-1168 / com.example.faku.ppt1 E / AndroidRuntime:   FATAL EXCEPTION: main       Process: com.example.faku.ppt1, PID: 1168       java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.faku.ppt1 / com.example.faku.ppt1.Pointer}:   java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.TextView.setText (java.lang.CharSequence) 'on a null   object reference           at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2665)           at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2726)           at android.app.ActivityThread.-wrap12 (ActivityThread.java)           at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1477)           at android.os.Handler.dispatchMessage (Handler.java:102)           at android.os.Looper.loop (Looper.java:154)           at android.app.ActivityThread.main (ActivityThread.java:6119)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:886)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.TextView.setText (java.lang.CharSequence) 'on a null   object reference           at com.example.faku.ppt1.Puntaje.mostrarrlista (Puntaje.java:66)           at com.example.faku.ppt1.Puntaje.resultado (Puntaje.java:53)           at com.example.faku.ppt1.Puntaje.onCreate (Puntaje.java:41)           at android.app.Activity.performCreate (Activity.java:6679)           at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1118)           at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2618)           at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2726)           at android.app.ActivityThread.-wrap12 (ActivityThread.java)           at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1477)           at android.os.Handler.dispatchMessage (Handler.java:102)           at android.os.Looper.loop (Looper.java:154)           at android.app.ActivityThread.main (ActivityThread.java:6119)

    
asked by Houth 21.05.2018 в 23:14
source

3 answers

2

The problem is a null in the TextView reference:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.TextView.setText (java.lang.CharSequence) 'on a null   object reference

You must set the reference to the variable txt0 before doing setText .

For example, if you are in Activity , in onCreate you do:

txt0 = findViewById(R.id.txt0);

Assuming that the id of TextView is txt0 in Layout . Anyway, your code will only show the last element in ArrayList , so you would not get the result you want. Since you are overwriting the text when iterating the list.

If you want a TextView that prints all the results separated by a ("|") symbol, you can do this:

String resultados = "";
for (int i = 0; i < list.size(); i++)
     if(i + 1 < list.size())
        resultados += list.get(i) + " | ";
     else
        resultados += list.get(i);

txt0.setText(resultados);

If you want to create as many TextViews as number of results in the list, you should create a ListView or a RecyclerView that has a% child% of%.

    
answered by 22.05.2018 / 00:13
source
0

You must use a arrayadapter to be able to show the content of the arraylist in the list view

I could do it like this:

        ListAdapter adaptador = new ArrayAdapter<>(nombredetuclase.this,android.R.layout.simple_list_item_1,nombredetuarraylist);
        ListaMateriales=(ListView) findViewById(R.id.ListaMateriales);
        ListaMateriales.setAdapter(adaptador);
    
answered by 21.05.2018 в 23:47
0

A possible solution to null would be to check the list size and send the value to the function that prints the result.

public void resultado(){
if (win) {
    result.setBackgroundResource(R.color.verde);
    result.setText("GANASTE!");
    result.setVisibility(View.VISIBLE);
    puntaje=String.valueOf("Ganaste en la jugada "+puntajefinal);
    list.add(0,puntaje);
    mostrarrlista();
} else {
    result.setBackgroundResource(R.color.rojo);
    result.setText("PERDISTE");
    result.setVisibility(View.VISIBLE);
    puntaje=String.valueOf("Perdiste en la jugada "+puntajefinal);
    list.add(0,puntaje);
    int a = list.size();
    mostrarrlista(a);
 }
}
    public void mostrarrlista(int a){
for (int i=0;i<=a;i++){

    txt0.setText(list.get(i));
    }
}
    
answered by 22.05.2018 в 00:17