ListView保持为空

时间:2014-03-19 01:15:36

标签: android list android-listview android-activity actionbarsherlock

我的result_page.xml布局中有一个ListView。

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

以下是我的活动片段

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);

当我尝试调试时,listView仍为null。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您使用的是SherlockListActivity而不是SherlockActivity。所以,你的布局必须是:

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

在你的onCreate方法中:

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);

        //... 
    }

}

设置适配器但未找到您的ID ListView,那么它应该有效!

相关问题