ListView yielding nullpointerexception on setOnItemClickListener

后端 未结 4 1294
南笙
南笙 2021-01-25 16:01

I\'m attempting to work with through some tutorial code and add in an OnItemClick Listener, but keep throwing an exception when it hits the listener and crashing my app. This is

4条回答
  •  爱一瞬间的悲伤
    2021-01-25 16:49

    ListActivity doesn't require you to assign a layout via setContentView() that is if you want to show only a list but if you add a another view, your ListView should contain the android:id attribute set to @android:id/list like this xml below

    
    
    

    try changing your code

    ListView lv = (ListView)findViewById(R.id.list);

    to this

    ListView lv = getListView();
    lv.setOnItemClickListener(newsSelectListener);
    

提交回复
热议问题