简体   繁体   English

FindViewById返回null?

[英]FindViewById returning null?

I am trying to populate a listView from an array. 我正在尝试从数组填充listView However, when I try and find the listview in my java code, the findViewById function returns null. 但是,当我尝试在Java代码中找到listview时,findViewById函数将返回null。

Here is my XML: 这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/androidbg_dark"
    tools:context=".DiaryActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lstAppts"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:textColor="#FFFFFF" ></ListView>

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_weight="1" 
            android:layout_height="64dp">

            <Button
                android:id="@+id/btnOther"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                android:onClick="newAppointment"
                android:textColor="#FFFFFF" />

            <Button
                android:id="@+id/btnOther2"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                android:onClick="newAppointment"
                android:textColor="#FFFFFF" />

            <Button
                android:id="@+id/btnNewAppt"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                android:onClick="newAppointment"
                android:text="@string/newappt"
                android:textColor="#FFFFFF" />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

And here is the offending code: 这是令人反感的代码:

ListView lstAppts = (ListView) findViewById(R.id.lstAppts);
String[] data = { "Appointment 1", "Appointment 2", "Appointment 3" };

ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
lstAppts.setAdapter(adapter);

To my knowledge, I am referencing the listview correctly. 据我所知,我正确地引用了列表视图。 It is named 'lstAppts' in my XML, and in the findViewById function, I find it using R.id.lstAppts . 在我的XML中,它被命名为“ lstAppts”,在findViewById函数中,我使用R.id.lstAppts找到了它。 However when I debug the code with breakpoints, I can clearly see the the lstAppts object is null . 但是,当我使用断点调试代码时,我可以清楚地看到lstAppts对象为null Why is this? 为什么是这样?

It seems like you're trying to call findViewById() on constructor. 似乎您正在尝试在构造函数上调用findViewById() In Android you should use onCreate() method to init your Activity instead of constructor. 在Android中,您应该使用onCreate()方法而不是构造函数来初始化您的Activity。

Most likely you didn't inflate the correct layout on the onCreate method. 您很可能没有在onCreate方法上膨胀正确的布局。 Check the setContentView method and make sure that it match the layout name of your "listView". 检查setContentView方法,并确保它与“ listView”的布局名称匹配。

If you are using listview in dialog,pop-up, etc where you have a parameter Context context type, you must use findViewById like this. 如果您在对话框,弹出窗口等中使用具有上下文上下文参数类型的列表视图,则必须使用findViewById这样。

ListView lstAppts = (ListView) context.findViewById(R.id.lstAppts);

Or if you have: View layout 或者,如果您有: 查看布局

ListView lstAppts = (ListView) layout.findViewById(R.id.lstAppts);

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM