2017-02-19 8 views
0

findViewById gibt null zurück, wenn nach einem ImageView in einem Fragment, aber nicht nach einem TextView gesucht wird.findViewById gibt manchmal null im Fragment zurück

Hier ist mein Code:

fragment.xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.herecomesthefragmentname"> 

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

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/card_1" 
      android:clickable="true" 
      android:foreground="?android:attr/selectableItemBackground"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:id="@+id/txt_1" 
       android:textAppearance="@android:style/TextAppearance.Medium" /> 
     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/2_card" 
      app:cardUseCompatPadding="true" 
      app:cardBackgroundColor="@android:color/white"> 
      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/mr_layout"> 
       <ImageView 
        android:id="@+id/2_imag" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:srcCompat="@drawable/img_1" 
        android:scaleType="fitStart" 
        android:adjustViewBounds="true" /> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/txt_2" 
        android:textAppearance="@style/TextAppearance.AppCompat.Large.Inverse" 
        /> 

      </RelativeLayout> 

      </android.support.v7.widget.CardView> 
     </LinearLayout> 
    <!-- here goes another cardview --> 
    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 

fragment.java (nicht alle):

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment, container, false); 
    return view; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 

    //Get Views 

    txt_container = (TextView) view.findViewById(R.id.txt_2); 
    img_view = (ImageView) view.findViewById(R.id.2_imag); 
} 

Das Merkwürdige ist, dass die txt_2 Ansicht gefunden wird, aber nicht die 2_imag eins.

Ich habe versucht, es umzubenennen, aber es tut nichts. Außerdem habe ich bemerkt, dass ich weder RelativeLayout noch CardView finden kann.

UPDATE: Es scheint, dass ich nicht einmal das Haupt CoordinatorLayout

Antwort

0

Gelöst eine Fragment-v21.xml Datei statt fragment.xml geladen wurde finden.

Die v21 hatte einen anderen Code.

Verwandte Themen