Java 如何使用有限的logcat调试(这个?)StackOverflowerError?

Java 如何使用有限的logcat调试(这个?)StackOverflowerError?,java,android,stack-overflow,Java,Android,Stack Overflow,我很抱歉,如果这是广泛的,不适当的或欺骗,请随时标记 然而,我找不到解决办法。我得到了StackOverflowerr,Android Studio显示- 08-03 16:22:49.293 5163-5163/com.package E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.StackOverflowError at android.view.ViewGroup.jumpDrawablesToCu

我很抱歉,如果这是广泛的,不适当的或欺骗,请随时标记

然而,我找不到解决办法。我得到了StackOverflowerr
,Android Studio显示-

08-03 16:22:49.293    5163-5163/com.package E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.StackOverflowError
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5384)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5388)
            at android.view.ViewGroup.jumpDrawablesToCurrentState(V
我现在花了几个小时试图弄明白哪里出了问题:我不知道代码中有任何递归调用。我在问:

  • 您是否对导致此特定问题的原因有任何建议
  • 对于如何在技术上面对类似问题,您有什么建议吗?有没有办法找到堆栈中的“根本”原因?应该有一些递归的东西在进行,但它肯定有(a)一个起点和对该起点的调用;(b) 当抛出
    StackOverflowerError
    时结束。我们可以看到结尾,但看不到开头,因为logcat停止了

我今天处理碎片时遇到了这个问题。此代码足以导致错误:

public static class ProductionFragment extends Fragment {   
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.something, container);
        return view;
    }
}
解决方案如下所示:

//The preferred way
View view = inflater.inflate(R.layout.something, container, false);

//Not optimal but still working way
View view = inflater.inflate(R.layout.something, null);
当我使用两个参数调用
inflater.inflate()
方法时,充气器会隐式地尝试将充气视图附加到容器。这会导致新创建的视图已经附加到片段,即使我们仍然没有返回视图

如果我们将null作为第二个参数传递,Android Studio会提醒我们

展开布局时,避免将null作为父视图传递, 否则,任何布局参数都将位于 布局将被忽略

从技术上讲,这促使我们填补了第二个参数

管理它的最佳方法是通过将容器参数传递给函数而不是将视图添加到根来跟踪容器参数。这是通过调用带有三个参数的
充气来完成的:

inflater.inflate(R.layout.something, container, false);
其中最后一个
false
告诉
inflate
方法不要将视图添加到我们提供的容器视图中

那么,为了防止将来出现这种错误,如何以编程方式决定
inflate
方法使用的重载

  • 您是否实现了一个方法,该方法要求您返回一个视图,而视图附加不是由您完成的?(片段,ListView/微调器的ListAdapter…)

    使用
    充气机充气(右布局、某物、容器、假)

  • 您是否从代码构建接口,并且您要加载的将是容器

    使用
    充气器充气(R.layout.something,空)

  • 您是否正在对计划手动添加到另一个容器中的内容进行充气

    使用
    充气机充气(右布局、某物、容器)并且不要将该项添加到容器中(它已经这样做了)


显示一些代码。这是StackOverflowerError,这意味着您正在不间断地递归调用相同的方法。它显然位于第
行的ViewGroup上。java:5388
您可以搜索具有相同问题的其他方法:。可能的解决方案:LayoutInflater.inflate([subViewOfViewPager],[ParentOfViewPager])更改为LayoutInflater.inflate([subViewOfViewPager],null)(这很可能是错误的,但您没有给出太多)代码在哪里?@ElDuderino这是一个很好的线索!我花了很多时间搜索,但主要关注堆栈溢出和调试方法。我想我的第一个问题已经解决了,我们可以把它留给第二个“最佳方法”问题,或者作为一个傻瓜来结束它。谢谢。我记不清了,但我的错误肯定与此有关。我认为这是对一个措辞拙劣的问题的一个很好的回答(恰当的标题应该是:当StackOverflow发生在框架本身内部调用的android SDK方法上时,如何调试StackOverflow,而您在堆栈跟踪中看不到您的任何内容?)。与上面的评论不同,你说到点子上了。