2013-07-15 2 views
0

배열에서 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임을 분명하게 알 수 있습니다. 왜 이런거야?

+0

게시물 logcat 출력. –

+4

제공하는 컨텍스트가 null 인 이유를 이해하는 것은 불가능합니다. 어쩌면 당신은 setContentView를 호출하는 것을 잊었을 것입니다. 예를 들어, – Blackbelt

+0

은 DiaryActivity 클래스의 자바 코드입니까? –

답변

0

대부분의 경우 onCreate 메서드에서 올바른 레이아웃을 사용하지 않았을 가능성이 큽니다. setContentView 메소드를 점검하고 "listView"의 레이아웃 이름과 일치하는지 확인하십시오.

0

매개 변수 컨텍스트 컨텍스트 유형이있는 대화 상자, 팝업 등에서 listview를 사용하는 경우 이와 같이 findViewById를 사용해야합니다. 당신이있는 경우

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

는 또는 : 보기 레이아웃

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

건배,

1

을 당신이 생성자에 findViewById()를 호출하려는 것 같다. Android에서는 생성자 대신 액티비티를 초기화하기 위해 onCreate() 메소드를 사용해야합니다.

관련 문제