gpt4 book ai didi

android - findViewById() 什么时候返回 null?

转载 作者:太空狗 更新时间:2023-10-29 15:39:40 27 4
gpt4 key购买 nike

我的 onCreate() 中有这个看似无辜的 findViewById(),它一直返回 null:

mCheckLicenseButton = (Button) findViewById(R.id.btn_checkout);

我的 res/layout/main.xml 清楚地包含这个按钮:

<Button android:id="@+id/btn_checkout" 
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/top_textview"
android:layout_alignParentRight="true"
android:text="Check License" />

所以我不明白为什么 findViewById() 返回 null。

什么情况下findViewById()会返回null?

很明显,findViewById() 没有成功找到布局资源,但有什么可能解释这个呢?

最佳答案

我要冒险说你在调用它之前没有调用 setContentView(R.layout.main),这是你必须做的。

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

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