Error returning to the main Activity

1

This happens to me when I use the app after closing it, the first time I open it I have no problems.

 E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.gep.guille.reportatemperatura, PID: 19524
              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=853211, result=-1, data=Intent { (has extras) }} to activity {com.gep.guille.reportatemperatura/com.gep.guille.reportatemperatura.MainActivity}: java.lang.UnsupportedOperationException
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
                  at android.app.ActivityThread.access$1400(ActivityThread.java:154)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5443)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
               Caused by: java.lang.UnsupportedOperationException
                  at java.util.AbstractList.add(AbstractList.java:404)
                  at java.util.AbstractList.add(AbstractList.java:425)
                  at com.gep.guille.reportatemperatura.MainActivity.onActivityResult(MainActivity.java:134)
                  at android.app.Activity.dispatchActivityResult(Activity.java:6470)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763) 
                  at android.app.ActivityThread.access$1400(ActivityThread.java:154) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5443) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

Here is the onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if ((requestCode == 853211) && (data !=null)) {

        String nombre=data.getStringExtra("NuevaCiudad");
        if(resultCode == RESULT_OK) {

            listaCiudades.add(nombre);//(MainActivity.java:134)
        }


        String jsonObjetos = new Gson().toJson(listaCiudades);
        Log.i("json",jsonObjetos);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("listaCiudades", jsonObjetos);
        editor.commit();
        adapter.notifyDataSetChanged();
        super.onActivityResult(requestCode, resultCode, data);
    }
}

and here the Activity where the Intent returns:

 public class NuevaCiudad extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    setContentView(R.layout.nueva_ciudad);
    super.onCreate(savedInstanceState);


}
public void AgregarCiudad(View view) {
    EditText nueva_ciudad = (EditText) findViewById(R.id.editText);
    String nuevaCiudad = nueva_ciudad.getText().toString();
    if (nuevaCiudad.length() != 0) {

        Intent data = new Intent();
        data.putExtra("NuevaCiudad", nuevaCiudad);

        setResult(RESULT_OK,data);
        finish();

    } else {

        Toast.makeText(this, getResources().getString(R.string.error_ingreso), Toast.LENGTH_LONG).show();

    }


 }
 }
    
asked by Los Milton 18.12.2016 в 21:17
source

1 answer

0

The problem was that the list in the onCreate was assigned from an Array with the asList (Array) method, but then I gave an error when using list.add () "as if the list was an Array". So assign the values of the list using a for.

In short, the asList (Array) method is used to assign the values of a list through an Array but then it seems that the list is like an Array (although it respects methods such as size ())

Greetings! and thank you!

    
answered by 20.12.2016 / 20:03
source