ListView yielding nullpointerexception on setOnItemClickListener

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

    I am not sure your code will work

    private OnItemClickListener newsSelectListener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            AlertDialog.Builder alert=new AlertDialog.Builder(RssActivity.this);
            alert.setTitle("Clicked").setMessage("Item clicked").setNeutralButton("OK", null).show();
    
        }
    };
    

    But in my opinion, I often add setOnClickListener() to convertView in getView method in Adapter class

    public View getView(final int position, View convertView, ViewGroup parent) {
    ......................
    convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //Do Somethings in here
                    }
                }
            });
    

提交回复
热议问题