Android studio “ Attempt to invoke virtual method on a null object reference”

前端 未结 2 1776
心在旅途
心在旅途 2020-12-20 21:53

when i create the custom view of each items of the list view, i get a null pointer exception and i dont know why, the layout id seems correct

import android.         


        
2条回答
  •  -上瘾入骨i
    2020-12-20 22:17

    Try changing the relevant code inside your Adapter class to the following

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView;
        if (convertView == null) {
           itemView = inflater.inflate(R.layout.item_view, parent, false); 
        } else {
           itemView = convertView;
        }
        ...
        ...
        return itemView;
    }
    

提交回复
热议问题