Error passing string strings to another activity

1

I have this code to receive a chain of strings that passes from another activity.

TextView infoEnviada;
    infoEnviada = (TextView) findViewById(R.id.reslt);
    String[] array = getIntent().getStringArrayExtra("resultados");
    infoEnviada.setText(array);
}

The error is in infoEnviada.setText(array); , says: Cannot resolve method setText(java.lang.String[])

This is the Android Monitor:

03-06 16:54:45.814 6978-6978/com.example.pablo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.NullPointerException
                                                                               at com.example.pablo.myapplication.encuesta$1.onClick(encuesta.java:59)
                                                                               at android.view.View.performClick(View.java:4162)
                                                                               at android.view.View$PerformClick.run(View.java:17088)
                                                                               at android.os.Handler.handleCallback(Handler.java:615)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                               at android.os.Looper.loop(Looper.java:137)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4867)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
                                                                               at dalvik.system.NativeStart.main(Native Method)

This is the else that gives error:

else
            {
                ListView listaresultados = (ListView) findViewById(R.id.reslt);
                ArrayList respuestas = new ArrayList();
                ArrayAdapter adaptador2 = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, respuestas);
                listaresultados.setAdapter(adaptador2);
                respuestas.add(strNombre);
                respuestas.add(curso);
                respuestas.add(strPregCuatro);
                respuestas.add(strPregCinco);
                Intent intent = new Intent (encuesta.this, res8.class);
                intent.putExtra("resultados", respuestas);
                startActivity(intent);
                Intent pas = new Intent(encuesta.this, MainActivity.class);
                Toast.makeText(context,"¡Encuesta enviada!",Toast.LENGTH_LONG).show();
                startActivity(pas);

            }
    
asked by Pablo Gonzalez 06.03.2017 в 22:25
source

2 answers

0
  

Can not resolve method setText (java.lang.String [])

You should check that it is the reslt element in your Layout, it is probably not a TextView.

 infoEnviada = (TextView) findViewById(R.id.reslt);

Originally reslt was a ListView therefore I mark this error.

Add values of an array in a TextView:

If it is an array, the option would be to convert the elements of the Array to String:

String[] array = { "unu", "doi", "trei", "patru", "cinci" };
textView.setText(Arrays.toString(array));   

Another option is to use StringBuilder to store the values of the Array:

 String[] array = { "unu", "doi", "trei", "patru", "cinci" };

StringBuilder builder = new StringBuilder();
for (String s: array) {
    builder.append(s); // Agrega elemento del array.
    builder.append(" "); //Separador.
}

textView.setText(builder.toString().trim()); //.trim() se usa para eliminar el último espacio.

Update:

Now the element reslt is a TextView so the ListView no longer exists, we do not need the lines that I comment ...

                //ListView listaresultados = (ListView) findViewById(R.id.reslt);
                ArrayList respuestas = new ArrayList();
                ArrayAdapter adaptador2 = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, respuestas);
                //listaresultados.setAdapter(adaptador2);
                respuestas.add(strNombre);
                respuestas.add(curso);
                respuestas.add(strPregCuatro);
                respuestas.add(strPregCinco);
                Intent intent = new Intent (encuesta.this, res8.class);
                intent.putExtra("resultados", respuestas);
                startActivity(intent);
                Intent pas = new Intent(encuesta.this, MainActivity.class);
                Toast.makeText(context,"¡Encuesta enviada!",Toast.LENGTH_LONG).show();
                startActivity(pas);
    
answered by 06.03.2017 в 22:38
0

The problem is that you are trying to invoke setText() with an array of Strings and that is not possible.

You can choose one of the elements of the array that you want to pass to the setText:

infoEnviada.setText(array[i]); //i = lugar de la cadena de texto en el array

Or you can show them all with a loop:

for (int i=0 ; i<=array.lenght ; i++){
    infoEnviada.setText(infoEnviada.getText().toString() + "\n" + array[i]);
}
    
answered by 18.03.2017 в 17:26