我的问题是,我在TabActivity的单个选项卡下有多个活动,但问题是如果我从一个活动移动到另一个活动超过三次,那时发生了“java.lang.StackOverflowError”,抱歉因为我的英语沟通不好。
任何帮助都将不胜感激。
我的代码是: -
FirstActivity.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);
}
}
错误Logcat: -
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
答案 0 :(得分: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();
要创建片段,只需扩展片段(还要确保持有片段的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
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
只有FirstActivity
应该从ActivityGroup
延伸,其余的应该像Activity
那样延伸SecondActivity
类,依此类推。不要从FirstActivity
启动SecondActivity
。
本教程中的更多细节:
http://www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity