首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findViewById()返回null -我应该做什么?

findViewById()返回null -我应该做什么?
EN

Stack Overflow用户
提问于 2018-09-16 07:43:17
回答 3查看 784关注 0票数 2

我在我的安卓项目中使用了一个bottomSheetBehavior。见下文代码:

onlineGame.java:

代码语言:javascript
复制
 // get the bottom sheet view
        ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);

// init the bottom sheet behavior
        end_of_online_game_popup = BottomSheetBehavior.from(llBottomSheet);

avtivity_online_game.xml:

代码语言:javascript
复制
.
.
.

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.androidsample.BottomSheetActivity">

        <!-- include bottom sheet -->
        <include
            android:id="@+id/includeBottomSheetBehavior"
            layout="@layout/test_end_of_online_game_popup" />

    </android.support.design.widget.CoordinatorLayout>
.
.
.

test_end_of_online_game_popup.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/end_of_online_game_bottom_sheet_behavior_cl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cardview_light_background"
    android:visibility="gone"
    app:behavior_hideable="false"
    app:behavior_peekHeight="120dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    .
    .
    .

问题是这条线:

代码语言:javascript
复制
ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);

返回null。我甚至将代码位置更改为onResume,但没有工作。当我在test_end_of_online_game_popup中获得另一个元素时,它工作得很好,而不是null。

有什么问题吗?

tnx

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-16 07:50:34

两种选择:

  1. ConstraintLayout llBottomSheet = findViewById(R.id. includeBottomSheetBehavior);
  2. <include layout="@layout/test_end_of_online_game_popup" />
票数 2
EN

Stack Overflow用户

发布于 2018-09-16 07:50:14

当您使用包含的布局时:

使用您给包含标记的id的find视图。

代码语言:javascript
复制
findViewById(R.id.includeBottomSheetBehavior)

或者,您可以在布局标签中省略id,这样它就不会被覆盖。

在< include >标记中,只需要布局属性。此属性是对希望包含的布局文件的引用。此标记还允许您覆盖包含的布局的一些属性。

上面的示例显示您可以使用android:id来指定所包含布局的根视图的id;如果定义了,它也将覆盖包含的布局的id。同样,您可以覆盖所有布局参数。

来源:http://www.curious-creature.com/2009/02/25/android-layout-trick-2-include-to-reuse/

票数 1
EN

Stack Overflow用户

发布于 2018-09-16 08:07:25

试试这个:

代码语言:javascript
复制
View view = findViewById(R.id.includeBottomSheetBehavior);//firstly get the root view ID
ConstraintLayout llBottomSheet = view.findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52352030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档