膨胀包含自己的视图的自定义视图时出现 StackOverflowError
StackOverflowError when inflating custom view which contains its own views
我有一个片段,我在其中充气 "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() returns null。
在 BoardView 中,我是这样充气的:
View view = inflate(getContext(), R.layout.view_board, this);
如前所述,我发现两个滚动视图在单独膨胀时都很好,但是当作为片段的一部分膨胀时,我得到 WhosebugError。
原因很清楚:我先在片段中膨胀,然后在视图中再次膨胀相同的 XML,这会触发视图的另一个 inflation,依此类推。我在这里得到一个循环引用。问题 我不知道如何将内部(水平)滚动视图添加到现有布局中。我想我需要以某种方式合并或手动扩充布局,但我不知道该怎么做。
这里有一些参考资料(对我的情况没有帮助):
- Android - Writing a custom (compound) component
- How to inflate XML-Layout-File correctly inside Custom ViewGroup?
- Create a custom View by inflating a layout?
- InvocationTargetException on inflating an xml - android
- Android: WhosebugError with InvocationTargetException when inflating layout
任何人都可以建议我应该做什么。如果可能的话,我希望 BoardView 作为一个通用组件工作,我可以在需要的地方将其插入到布局树中。
由于 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>
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
的更多信息
我有一个片段,我在其中充气 "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() returns null。
在 BoardView 中,我是这样充气的:
View view = inflate(getContext(), R.layout.view_board, this);
如前所述,我发现两个滚动视图在单独膨胀时都很好,但是当作为片段的一部分膨胀时,我得到 WhosebugError。
原因很清楚:我先在片段中膨胀,然后在视图中再次膨胀相同的 XML,这会触发视图的另一个 inflation,依此类推。我在这里得到一个循环引用。问题 我不知道如何将内部(水平)滚动视图添加到现有布局中。我想我需要以某种方式合并或手动扩充布局,但我不知道该怎么做。
这里有一些参考资料(对我的情况没有帮助):
- Android - Writing a custom (compound) component
- How to inflate XML-Layout-File correctly inside Custom ViewGroup?
- Create a custom View by inflating a layout?
- InvocationTargetException on inflating an xml - android
- Android: WhosebugError with InvocationTargetException when inflating layout
任何人都可以建议我应该做什么。如果可能的话,我希望 BoardView 作为一个通用组件工作,我可以在需要的地方将其插入到布局树中。
由于 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>
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
的更多信息