【问题标题】:ListView remains nullListView 仍然为空
【发布时间】:2014-03-19 01:15:35
【问题描述】:

我的 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。我错过了什么吗?

【问题讨论】:

  • 请将代码贴在ResultPageAdapter中
  • @VikramV 我的意思是说,即使在这一点上它也是空的 => listView = (ListView) findViewById(R.id.resultList);

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


【解决方案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,那么它应该可以工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2016-04-10
    相关资源
    最近更新 更多