Reputation: 525
i am trying to add a linear layout defined in xml at the footer of a listview. i tried the below code
listview.addFooter(findViewById(R.id.my_linearlayout))
but it is throwing an error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
if i just remove the addFooter code the acitvity runs smoothly.can one please suggest what is issue or if it is the right way to add the linearlayout at the footer of listview dynamically. thanks in advance.
Upvotes: 0
Views: 272
Reputation: 3352
You have to do this in way
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_footer_layout, null, false);
ListView.addFooterView(footerView);
here list_footer_layout name of xml file into in layout folder.
Layout for footer like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="@+id/list_footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="@string/footer_text_1"
android:id="@+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
</LinearLayout>
</LinearLayout>
Activity class could be
public class MyListActivty extends ListActivity {
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_footer_layout, null, false);
list.addFooterView(footerView);
}
}
Upvotes: 0
Reputation: 12552
You have to do it this way:
LinearLayout linearLayout = LayoutInflater.from(context).inflate(R.layout.your_layout, listview, false);
listview.addFooter(linearLayout);
Upvotes: 0
Reputation: 899
View footerView = getLayoutInflater().inflate(R.layout.my_linearlayout, null); listview.addFooterView(footerView);
You should add my_linearlayout to your /res/layout/ folder.
Upvotes: 2