gpt4 book ai didi

android - findViewById() 返回 null - 我该怎么办?

转载 作者:行者123 更新时间:2023-11-29 16:35:51 25 4
gpt4 key购买 nike

我在我的 android 项目中使用 bottomSheetBehavior。请参阅以下代码:

在线游戏.java:

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

.
.
.

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

<?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">
.
.
.

问题是这一行:

ConstraintLayout llBottomSheet = findViewById(R.id.end_of_online_game_bottom_sheet_behavior_cl);

返回 null。我什至将代码位置更改为onResume,但它没有用。当我在 test_end_of_online_game_popup 中获得另一个元素时,它运行良好并且不为空。

问题是什么?

发送

最佳答案

两种选择:

  1. ConstraintLayout llBottomSheet = findViewById(R.id. includeBottomSheetBehavior);

  2. <include layout="@layout/test_end_of_online_game_popup" />

关于android - findViewById() 返回 null - 我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com