开发者

When does findViewById() return null?

开发者 https://www.devze.com 2023-03-13 01:21 出处:网络
I have this seemingly innocent findViewById() in my onCreate() which keeps returning null: mCheckLicenseButton = (Button) findViewById(R.id.btn_checkout);

I have this seemingly innocent findViewById() in my onCreate() which keeps returning null:

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

My res/layout/main.xml clearnly contains this button:

<Button android:id="@+id/btn_checkout" 
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="w开发者_开发百科rap_content" 
        android:layout_below="@+id/top_textview"
    android:layout_alignParentRight="true"
    android:text="Check License" />

So I don't understand why findViewById() returns null.

In what circumstances does findViewById() return null?

Obviously, findViewById() doesn't succeed in finding the layout resource but what could possibly explain this?


I'm going to go out on a limb and say that you did not call setContentView(R.layout.main) before calling that, which you must do.


You need to call setContentView before:

setContentView(R.layout.main);
0

精彩评论

暂无评论...
验证码 换一张
取 消