Android堆栈内存错误(StackOverFlow)

时间:2015-04-08 09:19:51

标签: android stack views overflow

美好的一天。我有很多嵌套的布局,我用它来显示视图而不会杀死我的MainActivity视图。(这是Boss想要的)递归使用它们之后我来到了指向android按下后退按钮我必须按照想法去回到另一个嵌套视图,所以我再次以递归方式调用视图。在互联网上搜索我指出我无法增加android的堆栈内存并且我得到StackOverFlow错误主要是因为堆叠的嵌套视图更多比层次结构可以容纳。我的问题是如何从完全不同的布局中回到我的另一个嵌套布局,避免stackoverfowerror?(再次我的老板想要所有这些,所以我的功能很短)。非常感谢你事先! 发布LogCat和我用来添加视图的一段代码。

logcat的

java.lang.StackOverflowError
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.widget.ListView.drawChild(ListView.java:3302)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.widget.AbsListView.dispatchDraw(AbsListView.java:2384)
        at android.widget.ListView.dispatchDraw(ListView.java:3297)
        at android.view.View.draw(View.java:13824)
        at android.widget.AbsListView.draw(AbsListView.java:3881)
        at android.view.View.getDisplayList(View.java:12742)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.draw(View.java:13824)
        at android.widget.FrameLayout.draw(FrameLayout.java:467)
        at android.view.View.getDisplayList(View.java:12742)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.draw(View.java:13824)
        at android.widget.FrameLayout.draw(FrameLayout.java:467)
        at android.view.View.getDisplayList(View.java:12742)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java:12740)
        at android.view.View.getDisplayList(View.java:12786)
        at android.view.View.draw(View.java:13529)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3031)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2885)
        at android.view.View.getDisplayList(View.java

代码

context = Profile.this;
        activityManager = getLocalActivityManager();
        contentViewLayoutParams = new RelativeLayout.LayoutParams(switchlayout.getLayoutParams());
        nextActivit = new Intent(Profile.this, CardList.class);
        startGroupActivity("activity1", nextActivit);


  public void startGroupActivity(String id, Intent intent) {
    switchlayout.removeAllViews();
    View view = activityManager.startActivity(id, intent).getDecorView();
    switchlayout.addView(view, contentViewLayoutParams);

}

2 个答案:

答案 0 :(得分:0)

我认为您遇到了这个问题,因为您不会重复使用可见对象,并且每次可见对象都会创建新对象。 在这里看看http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance 8. ListViews和性能 你的问题可能与此有关。

答案 1 :(得分:0)

实现目标的唯一 PROPER 方式是使用Fragment。声明:

  

首先,这种或那种方式的片段使用其内部的代码   ,让我们说活动A有自己的片段片段A,所以在这   point Fragment代码必须写在Activity A

不对。你可以在Fragment里面有一个代码。但这不是主要观点。你说上面告诉我你的方式有什么不同吗?您有一个活动和许多视图。这并不意味着你已经拥有了活动中的所有代码吗?那么差异是什么呢? 如果你想调用父活动的方法,让我们说yerSmartMethod(),从Fragment你可以做到这么简单

((MainActivity)getActivity()).yerSmartMethod();

难道难吗?不。如果你想拥有不同片段的SAME代码,你所要做的就是用这些方法开发一些父片段并在所有片段中扩展它。你甚至可以将它宣布为抽象的。像:

public abstract class AbstractFragment extends Fragment{

    public void yerSmartMethod(){
       //if you still wish to use your Activity's method here:
       ((MainActivity)getActivity()).yerSmartMethod();
       // or u r free to code it
    }
}

现在是第一个片段:

public class My1stFragment extends AbstractFragment {
    ...
    //somewhere in code u wanna call this method, its just:
    yerSmartMethod();
    ...
}

所以你不需要在MainActivity中保留所有方法 这里唯一剩下的就是开发你自己的一堆片段,如果app goues bg那么片段堆栈就会丢失。其已知的AOS行为。但它很容易实现你自己的堆栈,它会像你的MainActivity一样存活下来。