1

我的问题是,我在 TabActivity 的单个选项卡下有多个活动,但问题是如果我从一个活动移动到另一个活动超过 3 次,则会发生“java.lang.StackOverflowError”,对不起我的不好英语交流。

任何帮助,将不胜感激。

我的代码是:-

第一活动.java

public class FirstActivity extends ActivityGroup implements
        OnClickListener {

    Button mBtnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);
        mBtnAdd = (Button) findViewById(R.id.mBtnAddManageAccount);
        mBtnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == mBtnAdd) {
            Intent mInSecond = new Intent(v.getContext(), SecondActivity.class);
            replaceContentView("SecondActivity", mInSecond);
        }
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }
}

SecondActivity.java

public class SecondActivity extends ActivityGroup implements
        OnClickListener {

    Button mBtnBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        mBtnBack=(Button)findViewById(R.id.mBtnBackNewCustomer);
        mBtnBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==mBtnBack){
            Intent mInBack = new Intent(v.getContext(),
                    FirstActivity.class);
            replaceContentView("FirstActivity", mInBack);
        }
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }
}

错误日志猫:-

07-19 11:02:01.731: E/AndroidRuntime(528): FATAL EXCEPTION: main
07-19 11:02:01.731: E/AndroidRuntime(528): java.lang.StackOverflowError
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6986)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.ScrollView.draw(ScrollView.java:1409)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-19 11:02:01.731: E/AndroidRuntime(528):  a
4

3 回答 3

2

ActivityGroup实际上已被弃用。您应该改用 Fragments。这将有助于管理您的内存,从而避免您的堆栈溢出错误。

基本上,您可以像第一项一样在 XML 中声明您的片段,也可以给它们一个占位符,就像第二项一样。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/fragment"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

现在第一个片段将自动膨胀并显示。然而,@id/fragmentContainer 将需要膨胀,因为我们只是将它存根。

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

要创建一个片段,只需扩展 Fragment(同时确保持有该片段的 Activity 扩展了 FragmentActivity)。然后确保创建并返回要显示的视图,类似于为 ListView 行创建和返回视图的方式。

public class ExampleFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.example_fragment_layout);

            /* All your find view by id's */
            return view;
    }
}

更多信息可以在这里找到 http://developer.android.com/guide/components/fragments.html http://developer.android.com/reference/android/app/Fragment.html

于 2012-07-22T15:48:18.080 回答
1

这是我的解决方案

public class ActivityStack extends ActivityGroup {

  private Stack<String> stack;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (stack == null) stack = new Stack<String>();
    //start default activity
    push("FirstStackActivity", new Intent(this, FirstStackActivity.class));
  }

  @Override
  public void finishFromChild(Activity child) {
    pop();
  }

  @Override
  public void onBackPressed() {
    pop();
  }


  public void push(String id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
      stack.push(id);
      setContentView(window.getDecorView());
    }
  }

  public void pop() {
    if (stack.size() == 1) finish();
    LocalActivityManager manager = getLocalActivityManager();
    manager.destroyActivity(stack.pop(), true);
    if (stack.size() > 0) {
      Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
      Window newWindow = manager.startActivity(stack.peek(), lastIntent);
      setContentView(newWindow.getDecorView());
    }
  }
}

启动选项卡

Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId")
spec.setContent(intent);

调用下一个活动

public class FirstStackActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    textView.setText("First Stack Activity ");
    textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent intent = new Intent();
          intent.setClass(getParent(), SecondStackActivity .class);
          ActivityStack activityStack = (ActivityStack) getParent();
          activityStack.push("SecondStackActivity", intent);


      }
    });
    setContentView(textView);
  }
}

再次调用下一个

public class SecondStackActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    textView.setText("First Stack Activity ");
    textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent intent = new Intent();
          intent.setClass(getParent(), ThirdStackActivity .class);
          ActivityStack activityStack = (ActivityStack) getParent();
          activityStack.push("ThirdStackActivity", intent);


      }
    });
    setContentView(textView);
  }
}
于 2012-07-24T09:56:51.877 回答
0

只有FirstActivity应该从 扩展ActivityGroup,其余的应该是扩展Activity类,比如你的SecondActivity等等。不要启动FirstActivityfrom SecondActivity

本教程中的更多详细信息:

http://www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity

于 2012-07-21T09:28:44.223 回答