Android: listview crashes

前端 未结 2 1980
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2021-01-28 17:50

    Try like this.

    Public class ABCD implements OnItemClickListener{
              ListView listview;//class varible
    
               listview = (ListView)findViewById(R.id.list);
               setListAdapter(new ArrayAdapter<String>(MyIncidentActivity.this,
               R.layout.row_incident, R.id.label_incident, db_results));
               listview.setOnItemClickListener(this);
    
            @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题