【问题标题】:StackOverflowError when inflating custom view which contains its own views膨胀包含自己的视图的自定义视图时出现 StackOverflowError
【发布时间】:2015-07-29 10:19:30
【问题描述】:

我有一个片段,我在其中膨胀“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">

    <view
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.app.BoardView"
        android:id="@+id/view_board"/>
</FrameLayout>

如您所见,fragment_board 包含一个自定义视图“BoardView”,我想从中加载以下“view_board.xml”:

<com.app.BoardView 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>

</com.app.BoardView>

我的自定义视图包含两个滚动视图(我将其用于平移),并且我希望能够在其他布局中重新使用它。 BoardView 扩展了外部(垂直)滚动视图,如下所示:

public class BoardView extends ScrollView

当我单独使用它时,它会很好地膨胀,并且我可以在膨胀的布局中找到两个滚动视图。但是当我在布局树(例如片段)中使用它时,我遇到了问题。

在片段中,我像这样膨胀布局树:

View view = inflater.inflate(R.layout.fragment_board, container, false)

从片段中,我可以 findViewById() 外部(垂直)滚动视图,但 findViewById() 为内部(水平)滚动视图返回 null。

在 BoardView 中,我是这样充气的:

View view = inflate(getContext(), R.layout.view_board, this);

如前所述,我可以发现两个滚动视图在自己膨胀时都很好,但是当作为片段的一部分膨胀时,我得到 StackOverflowError。

原因很清楚:我先在片段中膨胀,然后在视图中再次膨胀相同的 XML,这会触发视图的另一次膨胀,依此类推。我在这里得到一个循环引用。问题我不知道如何将内部(水平)滚动视图添加到现有布局中。我想我需要以某种方式合并或手动扩展布局,但我不知道如何。

这里有一些参考文献(对我来说没有帮助):

任何人都可以建议我应该做什么。如果可能的话,我希望 BoardView 作为一个通用组件工作,我可以在需要的地方插入布局树。

由于 Android [根据上述参考之一] 不正式支持复合组件,我最好的选择是删除内部视图的 XML 布局并将它们添加到代码中吗?

【问题讨论】:

标签: android android-layout android-fragments android-inflate


【解决方案1】:

请检查这样的事情是否能解决问题:

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>

view_board.xml

 <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>

更多关于使用MERGE and INCLUDE的信息

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多