很抱歉,如果涉及范围太广,不合适或虚假,请随时举报。

但是我找不到解决方案。我收到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

我现在花了几个小时试图理解问题所在:我不知道代码中有任何递归调用。我在问:
  • 您对可能导致此特定问题的任何建议吗?
  • 您对如何从技术上面对类似问题有任何建议吗?有什么办法可以解决堆栈中的“根本”原因吗?应该有一些递归的东西,但是它肯定有(a)起点,并且有一个递归点; (b)结束,当StackOverflowError被抛出时。我们可以看到结尾部分,但看不到开始部分,因为logcat停止了。
  • 最佳答案

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

    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()方法时,充气器会隐式尝试将充气后的 View 附加到容器。即使我们仍然没有返回 View ,这也会导致新创建的 View 已经附加到 fragment 上。

    如果我们将null用作第二个参数,则Android Studio会提醒我们



    从技术上讲驱使我们填补第二个参数。

    最好的管理方法是通过将容器参数传递给函数来跟踪容器参数,而不是将我们的 View 添加到根目录中。这是通过使用三个参数调用inflate来完成的:
    inflater.inflate(R.layout.something, container, false);
    

    最后一个false告诉inflate方法不要将 View 添加到我们提供的容器 View 中。

    因此,为防止将来发生此错误,如何以编程方式确定inflate方法的重载使用的是什么?
  • 您是否在实现一种方法,要求您返回 View ,而 View 附加并不意味着您要这样做? ( fragment ,ListView/Spinners的ListAdapter ...)

    使用inflater.inflate(R.layout.something, container, false);
  • 您是否要通过代码构建接口(interface),并且要加载的内容将是容器?

    使用inflater.inflate(R.layout.something, null);
  • 您是否正在膨胀要手动添加到另一个容器的东西?

    使用inflater.inflate(R.layout.something, container);,不要将项目添加到容器中(它已经做到了)。
  • 10-04 17:15