2016-07-08 13 views
-4

In einem Fragment versuche ich eine Listenansicht einzurichten. In onCreateView():ListView ist null, warum?

mFindFriendsOptionsListView = (ListView) view.findViewById(R.id.find_friends_list_view); 
mFindFriendsOptionsAdapter = new FindFriendsOptionsAdapter(mFindFriendOptionIconIds, mFindFriendOptionTitles, mFindFriendOptionDescription); 
mFindFriendsOptionsListView.setAdapter(mFindFriendsOptionsAdapter); 

ist die XML für die Listenansicht:

<ListView 
     android:id="@+id/find_friends_options_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="32dp" 
     android:layout_alignParentTop="true" 
     android:divider="#999999" 
     android:dividerHeight="1dp"/> 

Hier ist die Liste Element XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<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="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <ImageView 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:id="@+id/option_icon_image_view" 
      android:layout_marginRight="16dp" 
      android:layout_marginEnd="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_marginLeft="16dp"/> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_gravity="center_vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/option_title_text_view" 
       tools:text="Contacts" 
       android:textSize="18sp" 
       android:layout_marginBottom="6dp"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/option_description_text_view" 
       tools:text="Upload contacts to be your friends" 
       android:textSize="14sp"/> 
     </LinearLayout> 

    </LinearLayout> 

    <ImageView 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:id="@+id/arrow_image_view" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="8dp" 
     android:layout_marginEnd="8dp" /> 

</RelativeLayout> 

Hier wird der Stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
    at com.hb.birthpay.fragment.FindFriendFragment.onCreateView(FindFriendFragment.java:66) 

Ich habe d ebugged und das Feld mFindFriendOptionsListView ist null, obwohl ich es mit findViewById() eingestellt habe. Warum ist das ListView-Feld leer und wie behebe ich das, sodass ich keine java.lang.NullPointerException mehr bekomme?

+0

Was ist der Wert von 'R.id.find_friends_list_view'? –

Antwort

3

Ich vermute, es ist, weil es eine falsche ID find_friends_options_list_view hat, und Sie bekommen es mit dieser ID:

mFindFriendsOptionsListView = (ListView) view.findViewById(R.id.find_friends_list_view); 

ändern entweder einen zum anderen entsprechen. Machen Sie es sich zur Gewohnheit, diese Dinge zu kopieren und einzufügen, sonst haben Sie eine schlimme Zeit.

+0

Danke für die blöde Frage. –

+0

Ah, passiert jedem. Mach dir keine Sorgen darüber. – Vucko

+1

Danke Kumpel, um fair zu sein Ich habe für 8 Stunden gerade so codiert, mehh nicht eine große Sache, es ist der Sommer hols. @Vucko –