Я пытаюсь заполнить listView
из массива. Однако, когда я пытаюсь найти listview в своем java-коде, функция findViewById возвращает значение null.FindViewById возвращает null?
Вот мой 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>
А вот код обижая:
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);
Насколько мне известно, я правильно ссылающийся на ListView. Он называется «lstAppts» в моем XML, а в функции findViewById
я нахожу его с помощью R.id.lstAppts
. Однако, когда я отлаживаю код с точками останова, я могу ясно видеть, что объект lstAppts - null
. Почему это?
Выход на выход логарифма. –
с контекстом, который вы предоставляете, невозможно понять, почему он равен нулю. Возможно, вы забыли позвонить в setContentView, например, – Blackbelt
- это код Java в классе DiaryActivity? –