배열에서 listView
을 채우려고합니다. 그러나 내 Java 코드에서 listview를 찾으려고하면 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);
, 나는 제대로 목록보기를 참조하고있다. 내 XML에서 'lstAppts'라는 이름으로, findViewById
함수에서 R.id.lstAppts
을 사용하여 찾을 수 있습니다. 그러나 중단 점을 사용하여 코드를 디버깅 할 때 lstAppts 객체가 null
임을 분명하게 알 수 있습니다. 왜 이런거야?
게시물 logcat 출력. –
제공하는 컨텍스트가 null 인 이유를 이해하는 것은 불가능합니다. 어쩌면 당신은 setContentView를 호출하는 것을 잊었을 것입니다. 예를 들어, – Blackbelt
은 DiaryActivity 클래스의 자바 코드입니까? –