1

我正在做一个项目,我需要在 Tabhost 的选项卡下放置一些活动。我正在使用tabgroupactivity我在互联网上某处找到的自定义示例,但现在我在完成我的活动时遇到了一点问题。首先是自定义类代码:

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

/*
 * The purpose of this Activity is to manage the activities in a tab.
 * Note: Child Activities can handle Key Presses before they are seen here.
 * @author Eric Harlow
 */
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);
        Intent lastIntent = manager.getActivity(lastId).getIntent();
        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();
          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();
      }
  }
}

所以基本上我的问题是当我在tabhost. 在第一个选项卡中,我有 3 个子活动,我们称它们Activity ABC。以下是有关活动之间连接的一些信息:

A --- can start -->B and C
B --- can start -->A and C
C --- can start -->A and B

我像这样开始他们:

Intent previewMessage = new Intent(A.this, C.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("ExpectSoon", previewMessage);

这就是问题所在:当我开始几次A,B,CA-->B-->C-->B-->C-->A-->C时没有问题......但是当我按下后退按钮时,我从我的 TagGroupActivity 的这条线上得到了 NullPointerException :

Intent lastIntent = manager.getActivity(lastId).getIntent();

这是 LogCat 输出:

10-05 10:51:30.995: ERROR/AndroidRuntime(20377): java.lang.NullPointerException
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.stampii.stampii.TabGroupActivity.finishFromChild(TabGroupActivity.java:49)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.app.Activity.finish(Activity.java:3420)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.stampii.stampii.TabGroupActivity.onBackPressed(TabGroupActivity.java:104)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.stampii.stampii.TabGroupActivity.onKeyUp(TabGroupActivity.java:89)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.KeyEvent.dispatch(KeyEvent.java:1527)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.app.Activity.dispatchKeyEvent(Activity.java:2210)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1779)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:811)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:811)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:811)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.widget.TabHost.dispatchKeyEvent(TabHost.java:278)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:811)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:811)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1812)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1160)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.app.Activity.dispatchKeyEvent(Activity.java:2205)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1779)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2758)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2730)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1999)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.os.Looper.loop(Looper.java:150)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at android.app.ActivityThread.main(ActivityThread.java:4293)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at java.lang.reflect.Method.invoke(Method.java:507)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
10-05 10:51:30.995: ERROR/AndroidRuntime(20377):     at dalvik.system.NativeStart.main(Native Method)

任何想法如何解决这个问题?

4

3 回答 3

4

我遇到了同样的问题。

但是经过调试,我才知道该行

parentActivity.startChildActivity(STRING_ID, intent);

负责NullPointerException

那是因为,您可能会使用相同STRING_ID的方法来启动多个子活动。因此,当 时onBackPressed,意图STRING_ID从列表中删除。

你应该打电话的方式startChildActivity

 parentActivity.startChildActivity(UNIQUE_STRING_ID, intent);

UNIQUE_STRING_ID必须不同

于 2012-02-06T10:13:57.473 回答
0
    Intent check= new Intent(getParent(),abcintent.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("abcActiivty", check);
于 2011-10-11T10:53:20.833 回答
0

我认为你必须这样开始

Intent intent = new Intent(A.this,B.class);
            TabGroupActivity parent=(TabGroupActivity)getParent();

            parent.startChildActivity("msg",intent);
于 2011-10-05T08:00:31.380 回答