Android: listview crashes

前端 未结 2 1981
陌清茗
陌清茗 2021-01-28 17:20

I\'m using android listview and its working perfectly fine. my implementation as below

ListView listview = (ListView)findViewById(R.id.list);
setListAdapter(new          


        
2条回答
  •  执笔经年
    2021-01-28 17:28

    Well from you code where you are using

    setListAdapter(adapter);
    

    indicates that you are using ListActivity, and while using ListActivity you get your list as follow:

    listView = this.getListView();
    

    or you can use

    listView = this.findViewById(android.R.id.list);
    

    and you are using

    listView = this.findViewById(R.id.list);
    

    which is wrong in case of ListActivity, and ListView cannot be found and results in NullPointerException.

提交回复
热议问题