0

BackPressed在使用ActivityGroupunder时遇到了一些问题TabHost。为了说明,首先将代码分层钻取如下:

TabGroupActivity 类

以下TabGroupActivity.java内容来自在线教程。

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 类

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

工具索引类

这是一个有 9 个按钮的页面,例如当按下按钮 1 时,它将调用Tools_1_diamond类:

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);   
} 

课内Tools_1_diamond_

布局中有一个可以返回Tools_index页面的按钮,如下:

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); 
}   

问题重现:

案例A: 1、在Tools_1_Diamond课堂上按自己设计的按钮时,(而不是后按)应用程序可以正常返回Tools_index页面。

  1. 然后,在Tools_index页面内部,如果此时用户按下 BackPressed 按钮,应用程序将返回Tools_1_Diamond页面,

  2. 然后再按 BackPress 按钮,FATAL EXCEPTION: main java.lang.NullPointerException就会出现错误。

案例B: 一路只用BackPress,不碰自行设计的后退键,一切运行顺畅。

问题/意图:

我想问题应该是自己设计的按钮并没有减少类似于BackPress方法的ArrayList......但是......

这怎么可能解决?我想向用户提供,当按下自行设计的返回按钮或 BackPress 方法时将返回Tools_index页面,并且在页面中进一步按下 BackPressTools_index将没有反应,甚至更好的是弹出alertDialog询问用户是否想退出应用程序。

日志猫:

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)
4

1 回答 1

0

第一的

问题可能在于TabGroupActivity,在您完成后:

mIdList.remove(index);

您不检查索引是否仍然有效,即大于或等于一。下面的代码应该是:

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.
}

第二

值得注意的是,它LocalActivityManager在 API 级别 13 中已被弃用,因此,除非您只针对 13 之前的设备,否则不应使用它。

即使您决定继续使用它,在某些情况下它实际上也不会启动新的Activity. 见这里

于 2013-06-19T16:19:06.223 回答