Android StackOverflowError在膨胀包含自己视图的自定义视图时出错
我有一个片段,在这里我膨胀“Fragment_board.xml”: 当我单独使用它时,它膨胀良好,我可以在膨胀的布局中找到两个滚动视图。但是,当我在布局树(例如片段)中使用它时,我遇到了问题 在片段中,我将布局树放大如下:Android StackOverflowError在膨胀包含自己视图的自定义视图时出错,android,android-layout,android-fragments,android-inflate,Android,Android Layout,Android Fragments,Android Inflate,我有一个片段,在这里我膨胀“Fragment_board.xml”: 当我单独使用它时,它膨胀良好,我可以在膨胀的布局中找到两个滚动视图。但是,当我在布局树(例如片段)中使用它时,我遇到了问题 在片段中,我将布局树放大如下: public class BoardView extends ScrollView View view = inflater.inflate(R.layout.fragment_board, container, false) View view = inflate(g
public class BoardView extends ScrollView
View view = inflater.inflate(R.layout.fragment_board, container, false)
View view = inflate(getContext(), R.layout.view_board, this);
从片段中,我可以在外部(垂直)滚动视图中找到findViewById(),但在内部(水平)滚动视图中,findViewById()返回null
在BoardView中,我的充气方式如下:
public class BoardView extends ScrollView
View view = inflater.inflate(R.layout.fragment_board, container, false)
View view = inflate(getContext(), R.layout.view_board, this);
如前所述,我可以发现两个滚动视图在单独膨胀时都很好,但当作为片段的一部分膨胀时,我得到了StackOverflowerError
原因很清楚:我首先在片段中膨胀,然后在视图中再次膨胀相同的XML,这会触发视图的另一次膨胀,以此类推。这里有一个循环引用。问题我不知道如何将内部(水平)滚动视图添加到现有布局中。我想我需要以某种方式合并或手动膨胀布局,但我不知道如何
以下是一些参考文献(对我的情况没有帮助):
由于Android[根据上面的一个参考文献]不正式支持复合组件,我的最佳选择是删除内部视图的XML布局并将其添加到代码中吗?请检查类似的方法是否可以解决此问题: fragment_board.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.app.BoardView
android:id="@+id/view_board"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="@+id/board_scrollview_vertical">
<android.widget.HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="@+id/board_scrollview_horizontal"
android:foregroundGravity="left">
</android.widget.HorizontalScrollView>
</merge>
view_board.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.app.BoardView
android:id="@+id/view_board"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="@+id/board_scrollview_vertical">
<android.widget.HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="@+id/board_scrollview_horizontal"
android:foregroundGravity="left">
</android.widget.HorizontalScrollView>
</merge>
是否可以尝试使用视图存根
?