如何使用有限的logcat调试(这?)StackOverflowError?

nat*_*rio 5 java stack-overflow android

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

但是我找不到解决方案.我收到了StackOverflowError,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
Run Code Online (Sandbox Code Playgroud)

我现在花了几个小时试图理解什么是错的:我不知道我的代码中有任何递归调用.我在问:

  • 您对可能导致特定问题的原因有什么建议吗?
  • 您对如何在技术上面临类似问题有任何建议吗?有没有办法到达堆栈中的"根"原因?应该有一些递归的东西,但它肯定有(a)一个起点,并呼吁这一点; (b)StackOverflowError抛出时的结束.我们可以看到结束部分,但不是开始,因为logcat停止了.

Bob*_*Bob 11

今天我有这个问题与片段一起工作.此代码足以导致错误:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

解决方案是以下之一:

//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);
Run Code Online (Sandbox Code Playgroud)

当我inflater.inflate()用两个参数调用方法时,inflater隐式地尝试将膨胀的视图附加到容器.这会导致新创建的视图已经附加到片段,即使我们仍然没有返回我们的视图.

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

在给布局充气时,避免传入null作为父视图,否则将忽略膨胀布局根部的任何布局参数.

技术上驱使我们填补第二个参数.

管理它的最佳方法是通过将容器参数传递给函数来跟踪容器参数,而不是将我们的视图添加到根目录.这可以通过调用inflate三个参数来完成:

inflater.inflate(R.layout.something, container, false);
Run Code Online (Sandbox Code Playgroud)

最后一个false告诉inflate方法不将视图添加到我们提供的容器视图.

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

  • 您是否正在实施一个方法,要求您返回一个视图,而附加视图并不是您不想做的?(片段,ListView/Spinners的ListAdapter ......)

    使用 inflater.inflate(R.layout.something, container, false);

  • 你是从代码构建你的界面,你正在加载什么将是容器?

    使用 inflater.inflate(R.layout.something, null);

  • 您是否正在计划手动添加到另一个容器的某些内容?

    使用inflater.inflate(R.layout.something, container);并且不要将项目添加到容器中(它已经完成).