很抱歉,如果涉及范围太广,不合适或虚假,请随时举报。
但是我找不到解决方案。我收到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
我现在花了几个小时试图理解问题所在:我不知道代码中有任何递归调用。我在问:
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
方法的重载使用的是什么?使用
inflater.inflate(R.layout.something, container, false);
使用
inflater.inflate(R.layout.something, null);
使用
inflater.inflate(R.layout.something, container);
,不要将项目添加到容器中(它已经做到了)。