Search code examples
androidlistandroid-listviewandroid-activityactionbarsherlock

ListView remains null


I have a ListView in my result_page.xml under layouts.

<ListView
        android:id="@+id/resultList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Below is a snippet of my activity

public class ResultActivity extends SherlockListActivity {
    ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_page);
        listView = (ListView) findViewById(R.id.resultList);
        ResultPageAdapter adapter = new ResultPageAdapter(this, resultBeanList);
        listView.setAdapter(adapter);

When I try to debug, listView remains null. Am I missing out something?


Solution

  • You are using a SherlockListActivity and not a SherlockActivity. So, your layout must be:

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

    And in your onCreate method:

    public class ResultActivity extends SherlockListActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.result_page);
    
            ResultPageAdapter adapter = new ResultPageAdapter(this, resultBeanList);
            setListAdapter(adapter);
    
            //... 
        }
    
    }
    

    Set you adapter without find your id's ListView, then it should work!