0

I'm having problems with two listviews, each one of them generated by an activity. From the listview A, a list of items, I can go to the listview B, options for an specific item of list A.

The problem is when I go from A to B, then back (using the Android back button) to A, and then try to go to B once again: (A --> B --back button-> A --> error when trying to go to B).

I really don't even know where to begin to search for the bug, so I wanted to ask here: anyone with the same error who solved it and can share the solution?

Thanks!

EDIT: I'm posting here the relevant code, as I said I don't know what could be causing the bug so I posted the two listviews:

LISTVIEW A: It has an image, a title of the emergency and details with the distance.

public class EmergenciesList extends ListActivity {
String[] listaEmergencias = new String[] { "Raoul Duffy con Wisconsin",
        "Mac Iver con Moneda", "Cdte. Malbec con Barnechea",
        "Av. Apoquindo con Pehuen", "Las Hualtatas con Indiana" };

String[] detalleEmergencias = new String[] { "< 50 mts", "2 km", "1,2 km",
        "3,4 km", "350 mts" };

private Integer[] imgid = { R.drawable.chico, R.drawable.mediano,
        R.drawable.grande, R.drawable.chico, R.drawable.mediano,
        R.drawable.grande };

RelativeLayout layr1;
Animation ar3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector<RowData>();

    for (int i = 0; i < listaEmergencias.length; i++) {
        try {
// RowData is just a private class for the title, details, and image.
            rd = new RowData(i, listaEmergencias[i], detalleEmergencias[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.add(rd);
    }

    adapter = new CustomAdapter(this, R.layout.second_list, R.id.title, data);
    setListAdapter(adapter);
    getListView().setTextFilterEnabled(true);
}

public void onListItemClick(ListView parent, View v, int position, long id) {
    adapter = (CustomAdapter) parent.getAdapter();
    data.removeAllElements();

    Intent singleEmergencyMenu = new Intent(EmergenciesList.this, EmergencyMenu.class);
    Bundle container = new Bundle();
    container.putInt("id", position);
    singleEmergencyMenu.putExtra("data2", container);
    startActivity(singleEmergencyMenu);
}
}

LISTVIEW B: It has a checkbox in the header and three elements in the list.

public class EmergencyMenu extends ListActivity {
String[] emergencia = new String[] { "Mostrar ubicación",
        "Carros asignados", "Archivos" };
final String SETTING_EMERGENCY = "id_actual_emergency";
private int emergencyID = -1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.emergencyID = getIntent().getBundleExtra("data2").getInt("id");

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    // A Header to have a single checkbox to select the emergency
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView header = (TextView) inflater.inflate(
            R.layout.list_reports_header, null);
    lv.addHeaderView(header, null, false);

    List<String> data = new ArrayList<String>();

    for (int i = 0; i < emergencia.length; i++) {
        try {
            data.add(emergencia[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    setListAdapter(new ArrayAdapter<String>(this, R.layout.emergency_menu,
            data));

    LoadSelection();

    CheckBox checkBox = (CheckBox) findViewById(R.id.select_emergency_id);
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            SaveSelection();
        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // Mostrar ubicación
            if (position == 1) {
                Toast.makeText(getApplicationContext(),"Mostrar ubicación", Toast.LENGTH_SHORT).show();
            }

            // Carros asignados
            else if (position == 2) {
                Toast.makeText(getApplicationContext(), "Carros asignados",Toast.LENGTH_SHORT).show();
            }

            // Archivos
            else if (position == 3) {
                Toast.makeText(getApplicationContext(),
                        "Archivos de la emergencia",Toast.LENGTH_SHORT).show();
            }

        }
    });
}

@Override
protected void onPause() {
    SaveSelection();
    super.onPause();
}

// save the selections in the shared preference in private mode for the
// user
private void SaveSelection() {
    CheckBox checkBox = (CheckBox) findViewById(R.id.select_emergency_id);
    if (checkBox.isChecked()) {
        SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = settingsActivity.edit();
        prefEditor.putInt(SETTING_EMERGENCY, emergencyID);
        prefEditor.commit();
    }
}

// Check the checkbox if this is the actual emergency (selected before).
private void LoadSelection() {
    SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);

    if (settingsActivity.contains(SETTING_EMERGENCY)) {
        // Checkbox de seleccionar la emergencia
        int id_actual = settingsActivity.getInt(SETTING_EMERGENCY, -1);
        if (emergencyID == id_actual) {
            CheckBox checkBox = (CheckBox) findViewById(R.id.select_emergency_id);
            checkBox.setChecked(true);
        }
    }
}

}

Tere
  • 245
  • 1
  • 3
  • 14
  • What about showing a stacktrace – mibollma Jul 04 '11 at 02:57
  • How can I get it? I tried yesterday but the Eclipse console only showed me the app deployment on the phone, I couldn't find the log. I'm on a mac, if it is also relevant. – Tere Jul 04 '11 at 16:26

2 Answers2

0

Maybe the problem is due to implement your intent, you must declare it in the Class, then in method you create it. You can post CODE the way you perform

PrDoHung
  • 21
  • 1
  • I added the code for the two listviews. You can see the intent in the onListItemClick for listview A ("EmergenciesList"). I didn't have a problem in other intents in my app with that way of doing it... – Tere Jul 04 '11 at 16:32
0

Are you building the B ListView each time or just showing it? Maybe the BACK button is "killing" it and then when you try to show it again it's null or it refers to something null. Show us the relevant code and any errors. Always good to use log to trace values, etc. I haven't had this particular problem.

karnok
  • 1,202
  • 1
  • 12
  • 17
  • I edited the question to show the code. Answering your question, I handled the "onPause()" event but it didn't solved the problem... or is there anything else to do so I can assure the B listview isn't killed but just resumed? – Tere Jul 04 '11 at 16:28
  • This is out of my league really, it looks like you have multiple acivities which I haven't done before. Still, you can either try to "catch" the BACK button (http://stackoverflow.com/questions/2000102/android-override-back-button-to-act-like-home-button) or maybe better is to put in your own "go back" button so you have control. BACK, as I've learned, actually means *ending* activities - onPause() might not get called. What error do you get anyway? Or it just doesn't work? – karnok Jul 05 '11 at 03:40
  • As I understand, the back button triggers the onPause() event, the activity is killed if other processes need more memory or it is no longer visible (look the activity lifecycle: http://developer.android.com/reference/android/app/Activity.html). I tried to put a manual back and it works! (thanks :) ) But I would really like to know what is the problem (besides one of the Android principles is "don't steal the back button" so I think is important...). I don't get any errors, it just stopped working, and I don't know how to get the log... – Tere Jul 05 '11 at 16:48
  • Yeah I have no idea what I'm talking about! Use LogCat. In Eclipse, Window -> Show View -> Other... -> LogCat or use the free app "aLogCat" from the market. – karnok Jul 06 '11 at 02:48
  • Yay! LogCat was exactly what I needed!! With that I got the stacktrace of the error, and the problem was with the adapter of the list A, which never get notified of the changes. A notifyDataSetChanged() solved it! Thank you for your help with the log! – Tere Jul 15 '11 at 02:13