0

I am facing a problem of application crash when I wanted to display a footer button underneath the list view. I have attached my xml code and activity code herewith. Any help will be highly appreciated.

ContactListActivity.java

  public class ContactListActivity1 extends ListActivity {
    static ArrayList<String> ids = new ArrayList<String>();

     ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ContactList1 contactList = this.getContacts();
        ArrayAdapter<Contact1> adapter = new ContactAdapter1(this,
                contactList.getContacts());


          // LoadMore button
            Button btnLoadMore = new Button(this);
            btnLoadMore.setText("Load More");

        View v = getLayoutInflater().inflate(R.layout.contactlistitem,null);

        lv.addFooterView(btnLoadMore);
        lv.setAdapter(adapter);



    }

And my XML code

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"


>

    <TextView
        android:id="@+id/txtDisplayName"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        >

</TextView>


     <ImageView

android:id="@+id/contact_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


/>



   <ListView
    android:id="@+id/contactList1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/txtDisplayName"
    />



</LinearLayout>
Mohan Timilsina
  • 490
  • 6
  • 25

1 Answers1

2

Yes, You made a mistake -

  • You're extending ListActivity so where is lv = getListView() in your code.

  • Or, you should give list = (ListView)findViewById(android.R.id.list); after your super.onCreate(savedInstanceState); in your onCreate

Because, extending ListActivity these are important. It seems not proper with your code. You better have look at this example How to add a footer in ListView? before starting your application.

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • `setContentView(...)` is not mandatory if you are extending Activity with ListActivity, is it? – Lalit Poptani Aug 23 '12 at 04:38
  • Just an addition, for it to work with a ListActivity, your listview should have the following id, `@android:id/list` – vikki Aug 23 '12 at 04:42
  • @SpK regarding your edit, getting a listview like that will break the layout structure, it'll have to be a child of the inflated view, i.e `v`, so the code should be `(ListView) v.findViewById(...)`. In any case the simplest way is to use `setContentView(..)` and then `getListView()`. – vikki Aug 23 '12 at 04:56