8

Ich kann Fehler hier nicht herausfinden. Ich kann einige Sache sein oevrlooking ...findViewById gibt null auf einem LinearLayout in einem <include> d view

Layout-XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/listLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" 
    android:orientation="vertical" > 

    <include 
     android:id="@+id/headerInclude" 
     android:layout_width="fill_parent" 
     android:layout_height="38dp" 
     android:layout_gravity="top" 
     layout="@layout/header" /> 

    <LinearLayout 
     android:id="@+id/actualView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="1" > 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:divider="@drawable/dotted_line" 
      android:dividerHeight="2dp" 
      android:drawSelectorOnTop="false" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp" /> 
    </LinearLayout> 

    <include 
     android:layout_width="fill_parent" 
     android:layout_height="35dp" 
     android:layout_gravity="bottom" 
     layout="@layout/footer" /> 

</LinearLayout> 

inklusive Header XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/headerLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/header_bgr" 
    android:orientation="horizontal" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:paddingTop="7dp" > 

    <TextView 
     android:id="@+id/tvScreenTitle" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.8" 
     android:ellipsize="end" 
     android:gravity="center|left" 
     android:singleLine="true" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#ffffff" 
     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/tvScreenSubTitle" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2" 
     android:gravity="center|right" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#ffffff" 
     android:textSize="15sp" 
     android:textStyle="bold" /> 

</LinearLayout> 

Liste Aktivität Java-Code:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    appData = ((DxApp) this.getApplicationContext()).getData(); 
    setContentView(R.layout.list); 
    setTitle("Some Title..."); 
    // some more code... 
    addButtons(); 
} 

protected void addButtons() { 
    LinearLayout headerLayout = (LinearLayout) findViewById(R.id.headerLayout); 
    Button btn = new Button(this); 
    btn.setText("Go to My Sets"); 
    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      // do some thing here... 
     } 
    }); 

    if (headerLayout != null) 
     headerLayout.addView(btn); 
} 

@Override 
public void setTitle(CharSequence title) { 
    TextView tvTitle = (TextView) findViewById(R.id.tvScreenTitle); 
    tvTitle.setText(title); 
} 

Wenn ich entfernen die Überprüfen Sie if(headerLayout != null), ich bekomme null Zeiger Ausnahme auf headerLayout.addView(btn).
Ich bin in der Lage TextView in Funktion setTitle zu bekommen, aber einige, wie nicht LinearLayout

Kann jemand mir helfen, herauszufinden, was mit dem Code ist falsch können ...

Antwort

28

Wenn Sie das id Attribut für das Set include Element, dann wird das die ID für die Wurzel des überhöhten Layouts sein. Anstatt nach einer LinearLayout mit der ID headerLayout zu suchen, suchen Sie nach der ID headerInclude (oder setzen Sie keine ID für das Element include).

+3

perfekt !! Vielen Dank, Luksprog :) – sadaf

0

versuchen Sie dies:

View v = getlayoutInflater().inflate(((LinearLayout)findViewById(R.id.headerInclude)), null); 
TextView tvTitle = (TextView) v.findViewById(R.id.tvScreenTitle);