适配器中的java.lang.IllegalArgumentException

时间:2015-12-24 01:37:43

标签: java android

我想列出使用此适配器接收的JSON数据,但是收到的错误表明他没有找到视图,我在日志中得到的错误是“java.lang.IllegalArgumentException:view must not not为空“

  public class categoriaAdapter extends ArrayAdapter {

    private List<categoriaModel> categoriaModelList;
    private int resource;
    private LayoutInflater inflater;

    public categoriaAdapter(Context context, int resource, List<categoriaModel> objects) {
        super(context, resource, objects);
        categoriaModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;




        if (convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.imgCategoria = (ImageView)convertView.findViewById(R.id.ivIcon);
            holder.catTitulo = (TextView)convertView.findViewById(R.id.tvMovie);
            holder.catDesc = (TextView)convertView.findViewById(R.id.tvTagline);
            holder.catID = (TextView)convertView.findViewById(R.id.tvYear);
            convertView.setTag(holder);

        } else{
            holder = (ViewHolder) convertView.getTag();
        }

        final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);

        ImageLoader.getInstance().displayImage(categoriaModelList.get(position).getImage(), holder.imgCategoria, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                progressBar.setVisibility(View.GONE);
            }
        });



        holder.catTitulo.setText(categoriaModelList.get(position).getNome());
        holder.catDesc.setText(categoriaModelList.get(position).getDescricacao());
        holder.catID.setText(categoriaModelList.get(position).getIdeCategoria());



        return convertView;
    }

    class ViewHolder{
        ImageView imgCategoria;
        TextView catTitulo;
        TextView catDesc;
        TextView catID;
    }
}

而我的 Error logs

2 个答案:

答案 0 :(得分:1)

请检查您的布局(xml)并查看R.id.ivIcon是否为imageView,或者它是否存在于resource布局中。在调用imgCategoria之前,请确保displayImage不为空且已正确初始化。

还初始化ImageLoader配置。即。

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
 ImageLoader.getInstance().init(config);
 ImageLoader.getInstance().displayImage(categoriaModelList.get(position).getImage(), holder.imgCategoria, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {
            progressBar.setVisibility(View.GONE);
        }
    });

希望它可以帮助你..

答案 1 :(得分:0)

检查是否包括正确的项目布局。我遇到了同样的问题,事实是我正在使用不同活动的项目布局。而且我已经复制了适配器代码。