1

I'm completely stumped on this one. I have an activity C, when I try and press the back button, it works. But when I use the home/up button in the action bar it just crashes with (see the following error). Here are the parts of my code which deal with the back/up buttons.

Activity C:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offline_viewer);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

    @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(!isOnline) {
        menu.removeItem(R.id.saveRoute);
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.offline_viewer, menu);
    return true;
}

@Override
public void onBackPressed() {
    super.onBackPressed(); 
    finish();
} 

@Override
public boolean onOptionsItemSelected(MenuItem item)
{       
    //Get names for saving
    String[] startParts = onlineFrom.split(",");
    String startName = startParts[0] + "," + startParts[1]; 
    String[] endParts = onlineTo.split(",");
    String endName = endParts[0] + "," + endParts[1]; 
    System.out.println(item.getItemId());
    switch(item.getItemId()){
    //Save xml file or route once pressed
    case R.id.saveRoute:
        //TODO:Uncomment once server is ready
        new DownloadFileFromURL(this, startName, endName).execute(urlForDownload);
        return true;       
    case android.R.id.home: 
        System.out.println(item.getItemId());
        onBackPressed();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Error:

08-22 16:15:43.629: W/dalvikvm(4908): threadid=1: thread exiting with uncaught exception     (group=0x41c7e888)
08-22 16:15:43.634: E/AndroidRuntime(4908): FATAL EXCEPTION: main
08-22 16:15:43.634: E/AndroidRuntime(4908): java.lang.NullPointerException
08-22 16:15:43.634: E/AndroidRuntime(4908):     at     com.example.otpxmlgetter.OfflineViewer.onOptionsItemSelected(OfflineViewer.java:185)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.app.Activity.onMenuItemSelected(Activity.java:2590)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.view.View.performClick(View.java:4204)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.view.View$PerformClick.run(View.java:17354)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.os.Handler.handleCallback(Handler.java:725)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.os.Looper.loop(Looper.java:137)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at android.app.ActivityThread.main(ActivityThread.java:5232)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at java.lang.reflect.Method.invoke(Method.java:511)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
08-22 16:15:43.634: E/AndroidRuntime(4908):     at dalvik.system.NativeStart.main(Native Method)

Does this have something to do with the fact that there are two possible ways to get to Activity C? Either A->B->C or A->D->C? The fact that the back button still works completely confounds me.

Pete B.
  • 3,188
  • 6
  • 25
  • 38
linus
  • 481
  • 5
  • 18

1 Answers1

3

It is commonly not recommended to call Activity life cyle methods. The onWhatEver methods are used by the framework. If you want to finish the activity rather use ...

this.finish()

or ...

getActivity.finish()

if you are in a fragment context.

p.s.s: onBackPressed does actually work (against initial asumption of this answer). See comments below for the root cause of this issue.

p.s.: this post proposes this.dispatchKeyEvent(new Keyevent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); as an alternative

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • i tried replacing the `onBackPressed();` with `this.finish()` and `OfflineViewer.this.finish();` but still i get the same error. – linus Aug 22 '13 at 08:39
  • Did you debug the code and check whether all of your member are not null (`onlineFrom`, `startParts[...]`, `onlineTo`, ...)? Another question: do you use the ActionBarSherlock library? If yes, take care that you have the right `MenuItem` in the method header (`com.actionbarsherlock.view.MenuItem`). – Trinimon Aug 22 '13 at 08:52
  • man that was so stupid on my part. When i did this ` menu.removeItem(R.id.saveRoute);` i forgot about the other members related to it, just needed to check if the startPart where null. Thank you very much! – linus Aug 22 '13 at 08:55
  • @linus: as per this post (http://stackoverflow.com/questions/10718789/how-to-press-back-button-in-android-programatically) `onBackPressed()` should work as well, though it is not really recommended. Have you tried this again? I'd update my answer slighty in this case in order to avoid confusions ;) – Trinimon Aug 22 '13 at 09:05
  • I tried both `onBackPressed()` and `finish()` and they both work. And yes, they are the same since `onBackPressed()` still calls finish according to the post. – linus Aug 22 '13 at 09:09
  • Ok I see - read once, that calling some of these `on...` methods does not work. So this was my first idea. Thx for the hint! – Trinimon Aug 22 '13 at 09:13