androidnullpointerexceptionactivitygroup

Android GroupActivity Backpress trigger Fatal Exception / NullPointerException


I have some problems on BackPressed when using the ActivityGroup under TabHost. To illustrate, first of all, codes are drilled down in layers as follows:

TabGroupActivity class

The following TabGroupActivity.java is referenced from an online tutorial.

public class TabGroupActivity extends ActivityGroup 
{    
    private ArrayList<String> mIdList;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);       
        if (mIdList == null) mIdList = new ArrayList<String>();
    }

    /**
     * This is called when a child activity of this one calls its finish method. 
     * This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity
     * and starts the previous activity.
     * If the last child activity just called finish(),this activity (the parent),
     * calls finish to finish the entire group.
     */
  @Override
  public void finishFromChild(Activity child) 
  {
      LocalActivityManager manager = getLocalActivityManager();
      int index = mIdList.size()-1;

      if (index < 1) 
      {
          finish();
          return;
      }

      manager.destroyActivity(mIdList.get(index), true);
      mIdList.remove(index); index--;
      String lastId = mIdList.get(index);
      Activity a = manager.getActivity(lastId); 
      Intent lastIntent = a.getIntent();      // LINE 49
      Window newWindow = manager.startActivity(lastId, lastIntent);
      setContentView(newWindow.getDecorView());
  }

  /**
   * Starts an Activity as a child Activity to this.
   * @param Id Unique identifier of the activity to be started.
   * @param intent The Intent describing the activity to be started.
   * @throws android.content.ActivityNotFoundException.
   */
  public void startChildActivity(String Id, Intent intent) 
  {     
      Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
      if (window != null) 
      {
          mIdList.add(Id);
          setContentView(window.getDecorView()); 
      }    
  }

  /**
   * The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
   * from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
   */
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) 
  {
      if (keyCode == KeyEvent.KEYCODE_BACK) 
      {
          //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }

  /**
   * Overrides the default implementation for KeyEvent.KEYCODE_BACK 
   * so that all systems call onBackPressed().
   */
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) 
  {
      if (keyCode == KeyEvent.KEYCODE_BACK) 
      {
          onBackPressed(); //LINE 88
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

  /**
   * If a Child Activity handles KeyEvent.KEYCODE_BACK.
   * Simply override and add this method.
   */
  @Override
  public void  onBackPressed  () 
  {
      int length = mIdList.size();
      if ( length > 1) 
      {
          Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
          current.finish(); //LINE 103
      }  
  }
}

Tools_GroupActivity class

public class Tools_GroupActivity extends TabGroupActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        startChildActivity("Tools_index", new Intent(this, Tools_index.class));
    }
}

Tools_index.class

It is a page with 9 buttons, for example when pressing button 1, it will invoke the Tools_1_diamond claass:

public void tools_button1_click (View view) 
{
    Intent edit = new Intent(getParent(), Tools_1_Diamond.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("Tools_1_Diamond", edit);   
} 

Inside Tools_1_diamond class

The layout has a button that can back to the Tools_index page, as follows:

public void button_tools_subpage_back_click (View view)   
{  
    Intent edit = new Intent(getParent(), Tools_index.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("Tools_index", edit); 
}   

@Override  
public void onBackPressed()   
{  
    Intent edit = new Intent(getParent(), Tools_index.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("Tools_index", edit); 
}   

Problem reproduced:

Case A: 1. When pressing self-designed button in Tools_1_Diamond class, (instead of the backpressed) the app can return properly back to the Tools_index page.

  1. Then, inside the Tools_index page, if the user at this time press the BackPressed Button, the app will go back to Tools_1_Diamond page,

  2. and then further press the BackPress Button, error FATAL EXCEPTION: main java.lang.NullPointerException will occur.

Case B: When all the way just using BackPress without touching the self-designed back button, everything runs smoothly.

Question / Intention:

I think the problem should be the self-designed button does not reduce the ArrayList similar to BackPress method...but...

How could this be solved? I would like to offer user that when pressing either the self-designed back button or the BackPress method will return to the Tools_index page, and further pressing the BackPress in the Tools_index page will offer no reaction, or even better, to pop up an alertDialog asking user whether want to quit the app.

Logcat:

06-19 22:40:03.570: D/webviewglue(23395): nativeDestroy view: 0x51909960
06-19 22:40:06.585: D/AndroidRuntime(23395): Shutting down VM
06-19 22:40:06.585: W/dalvikvm(23395): threadid=1: thread exiting with uncaught exception (group=0x40fe72a0)
06-19 22:40:06.670: E/AndroidRuntime(23395): FATAL EXCEPTION: main
06-19 22:40:06.670: E/AndroidRuntime(23395): java.lang.NullPointerException
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.abc.abc_v1.TabGroupActivity.finishFromChild(TabGroupActivity.java:49)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.app.Activity.finish(Activity.java:4215)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.abc.abc_v1.TabGroupActivity.onBackPressed(TabGroupActivity.java:103)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.abc.abc_v1.TabGroupActivity.onKeyUp(TabGroupActivity.java:88)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.KeyEvent.dispatch(KeyEvent.java:2729)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2033)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.widget.TabHost.dispatchKeyEvent(TabHost.java:309)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1363)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2106)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1466)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.app.Activity.dispatchKeyEvent(Activity.java:2426)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2033)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3852)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3800)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2935)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.os.Looper.loop(Looper.java:137)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at android.app.ActivityThread.main(ActivityThread.java:4921)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-19 22:40:06.670: E/AndroidRuntime(23395):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • First

    The problem could be that in TabGroupActivity, after you have done:

    mIdList.remove(index);
    

    you don't check that index is still valid, ie bigger than or equal to one. The following code should probably be:

    if (index > 0) {
         index--;
         String lastId = mIdList.get(index);
         Intent lastIntent = manager.getActivity(lastId).getIntent(); //LINE49
         Window newWindow = manager.startActivity(lastId, lastIntent);
         setContentView(newWindow.getDecorView());
    }
    else {
        // Handle case where there are no children left.
    }
    

    Second

    It is worth noting that LocalActivityManager was deprecated in API level 13, and so, unless you are only targeting pre-13 devices, it shouldn't be used.

    Even if you do decide to proceed with using it, there are some conditions under which it won't actually start a new Activity. See here.