ListView yielding nullpointerexception on setOnItemClickListener

后端 未结 4 1363
南笙
南笙 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:38

    EDIT

    Ok - and where are you setting setContentView(...)? The lv

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

    is null because you didn't set contentView.


    Probably the reason for this is: you're setting OnClickListener to null in AlertDialog.Builder in setNeutralButton("OK", null).

    So when you click "OK" Android invokes (internally) something like this:

    neutralButtonListener.onClick(...);
    

    And the neutralButtonListener is null. So you should just provide empty listener at least.

提交回复
热议问题