Domanda

I'm using the master/detail template in my Android app which works well on the tablet. However, in the handset version I keep getting a nullpointer exception when clicking the "up" button in the ListActivity.

When debugging I see that when clicking the "up" button in the detail fragment I return to the parent activity (the listactivity) and get back into the onCreate() method. In there I'm doing:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gamelist);
    String systemStr = getIntent().getStringExtra("systemObj");
    ...
}

But when returning to this from the detail fragment the intent is null and therefore I'm getting a nullpointer exception when accessing systemStr.

What do I need to do in order to avoid this nullpointer exception when I click "up" in the detail fragment to get back to the list activity? As mentioned above, the tablet (two pane) version works, the problem is just in the handset version.

È stato utile?

Soluzione

I couldn't find any solution for the problem so I ended up using this code in the detail activity to avoid the nullpointer exception:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // When pressing the UP button in the action bar simulate the "back" button.
    case android.R.id.home:
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

At least that solution won't crash my app.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top