Luke Batley
Luke Batley

Reputation: 2402

android accessing includes within another layout

Hi i'm trying to create a list view with a header view that i have included in the layout. Problem is i'm getting null pointer exceptions from the textview within my header. i assume this is because it can't find it so my question is how do access included elements within another layout?

heres my layout for my activity

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <include layout="@layout/listview_header" 
      android:id="@+id/listheader" />

   <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   />

</TableLayout>

heres my included list view_header

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:padding="10dp"
    android:id="@+id/header"
    >


     <TextView android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textSize="14dp"
        android:textColor="#ffffff"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" 
        android:shadowColor="@color/Black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="0.01"/>

</LinearLayout>

and heres my java

      setContentView(R.layout.lo_listview_with_header);
      gilSans = Typeface.createFromAsset(getAssets(), "fonts/gillsans.ttf");
      listView = (ListView) findViewById(android.R.id.list);
      header = getLayoutInflater().inflate(R.layout.listview_header, null);
      title = (TextView) header.findViewById(android.R.id.title);


      Bundle intent = getIntent().getExtras();
      int position = intent.getInt("position") + 1;
      String backgroundColor = intent.getString("bg_color");
      String titleText = intent.getString("title");


      TextView tvTitle = (TextView)  header.findViewById(R.id.title);
      header.setBackgroundColor(Color.parseColor(backgroundColor));
      title.setText(titleText);

  }

Upvotes: 0

Views: 66

Answers (1)

Mark
Mark

Reputation: 1996

I see two problems.

First, you are inflating a new listview_header, when what you actually want to be doing is getting the listview_header that's already included in the layout for your activity.

What you're doing:

header = getLayoutInflater().inflate(R.layout.listview_header, null);

What you want to be doing:

header = findViewById(R.id.listheader);

Second, you're using the wrong ID to find your title; you want R.id.title, not Android.R.id.title. When you use @+id/ in XML, your Java will use R.id; when you use @android:id/ in XML, your Java will use android.R.id. So:

  • @+id/id_name => findViewById(R.id.id_name);
  • @android:id/id_name => findViewById(android.R.id.id_name);

So what you're currently doing:

title = (TextView) header.findViewById(android.R.id.title);

And what you want to be doing:

title = (TextView) header.findViewById(R.id.title);

Hope this helps.

Upvotes: 1

Related Questions