Create intent with the id of a list

1

Good morning, I hope you can help me. I'm doing an internship on Android, and I'm stuck in a chapter about Activity and intents:

I have a List with several elements (places). I open a dialogue box where I enter the id and I open VistaLugar, where I can see the info of the place.

The problem comes at the time of opening the activity "Place Edition". In practice I must do that when pressing the Edit button from VistaLugar the "EditLink" view is opened, passing the id of the element that was open in the activity.

**

  

Unable to start activity   ComponentInfo {com.example.dks.mislugaresdomingo / com.example.dks.mislugaresdomingo.EdicionLugar}:   java.lang.ArrayIndexOutOfBoundsException: length = 10; index = -1

**

The question is: what happened to the current id (if that is what is missing, of course)

**

public class VistaLugar extends ActionBarActivity {
    private long id;
    private Lugar lugar;

@Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vista_lugar);
    Bundle extras = getIntent().getExtras();
    id = extras.getLong("id", -1);
    lugar = Lugares.elemento((int)id);

..............
    public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        ................
        case R.id.accion_editar:
            lanzaEdicionLugar((int)id);
            return true;

....



     public void lanzaEdicionLugar(int id) {
        Intent i = new Intent(VistaLugar.this, EdicionLugar.class);
        i.putExtra("id", id);
        startActivity(i);

    }
**

But I can not solve the problem. When it is executed from the other view VistaLugar this if it receives the parameters that have been entered from an AlertDialog:

public void lanzarVistaLugar(View view) { final EditText entrada = new EditText(this); ...... public void onClick(DialogInterface dialog, int witchButton){ long id = Long.parseLong(entrada.getText().toString()); Intent i = new Intent(MainActivity.this, VistaLugar.class); i.putExtra("id", id); startActivity(i);         
public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.menu_buscar) { lanzarVistaLugar(null); return true; }
    
asked by Berni Del Gado 18.04.2017 в 13:59
source

2 answers

0

You are doing an Intent from Activity VistaLugar to open EdicionLugar , if you notice the error indicates where the problem occurs, which is in class EdicionLugar :

  

Unable to start activity   ComponentInfo {com.example.dks.mislugaresdomingo / com.example.dks.mislugaresdomingo.EdicionLugar}:   java.lang.ArrayIndexOutOfBoundsException: length = 10; index = -1

The intent to send the id is correct and it is done without problem:

public void lanzaEdicionLugar(int id) {
    Intent i = new Intent(VistaLugar.this, EdicionLugar.class);
    i.putExtra("id", id);
    startActivity(i);

}

The problem is surely that when receiving EdicionLugar an index -1 , this index does not exist in an array of elements, it simply ensures to send a valid index!

Ok in the class VistaLugar you receive a bundle with the id but if you do not have this value, the value of -1 is obtained by default,

id = extras.getLong("id", -1);

change the default to 0 to be a valid index value:

id = extras.getLong("id", 0);
    
answered by 18.04.2017 / 16:57
source
0

As I understand that getLong -1 is to give a value when it does not exist, so if it does not exist it will close the app.

The conclusion is that in these cases knowing how to use the debugger is very important. The problem was that I was sending null values to the object from a textView of the activity.

Thanks for the contributions!

    
answered by 20.04.2017 в 17:22