arrays.sort在java中返回null

时间:2016-09-28 22:25:40

标签: java android

我在android studio中使用了相机api2我实现了recyclerview适配器,这是我的适配器是否建议添加notifychangedAdapter?我应该在哪里添加它们?:

public static class AsyncDrawable extends BitmapDrawable {
    final WeakReference<BitmapWorkerTask> taskReference;

    public AsyncDrawable(Resources resources,
                         Bitmap bitmap,
                         BitmapWorkerTask bitmapWorkerTask) {
        super(resources, bitmap);
        taskReference = new WeakReference(bitmapWorkerTask);
    }

    public BitmapWorkerTask getBitmapWorkerTask() {
        return taskReference.get();
    }
}

public ImageAdapter(File[] folderFiles, RecyclerViewClickPositionInterface positionInterface) {
    mPositionInterface = positionInterface;
    mImageFiles= folderFiles;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.gallery_images_relative_layout, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    File imageFile = mImageFiles[position];
    Bitmap bitmap = CamaraIntentActivity.getBitmapFromMemoryCache(imageFile.getName());
    if(bitmap != null) {
        holder.getImageView().setImageBitmap(bitmap);
    }
    else if(checkBitmapWorkerTask(imageFile, holder.getImageView())) {
        BitmapWorkerTask bitmapWorkerTask = new BitmapWorkerTask(holder.getImageView());
        AsyncDrawable asyncDrawable = new AsyncDrawable(holder.getImageView().getResources(),
                placeHolderBitmap,
                bitmapWorkerTask);
        holder.getImageView().setImageDrawable(asyncDrawable);
        bitmapWorkerTask.execute(imageFile);
    }
}

@Override
public int getItemCount() {
    return mImageFiles != null ? mImageFiles.length : 0;

}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private ImageView imageView;

    public ViewHolder(View view) {
        super(view);

        view.setOnClickListener(this);
        imageView = (ImageView) view.findViewById(R.id.imageGalleryView);
    }

    public ImageView getImageView() {
        return imageView;
    }

    @Override
    public void onClick(View v) {
        mPositionInterface.getRecyclerViewAdapterPosition(this.getPosition());
    }
}

public static boolean checkBitmapWorkerTask(File imageFile, ImageView imageView) {
    BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
    if(bitmapWorkerTask != null) {
        final File workerFile = bitmapWorkerTask.getImageFile();
        if(workerFile != null) {
            if(workerFile != imageFile) {
                bitmapWorkerTask.cancel(true);
            } else {
                // bitmap worker task file is the same as the imageview is expecting
                // so do nothing
                return false;
            }
        }
    }
    return true;
}

public static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    if(drawable instanceof AsyncDrawable) {
        AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
        return asyncDrawable.getBitmapWorkerTask();
    }
    return null;
}

在我的活动中,我像这样实现并在之后调用notifydatachanged:

 RecyclerView.Adapter imageAdapter = ImageAdapter(sortFilesToLatest(mGalleryFolder), this);
        mRecyclerView.setAdapter(imageAdapter);
        imageAdapter.notifyDataSetChanged();

这是我的sortFilesToLatest,它对使用相机后拍摄的图像进行排序

 private File[] sortFilesToLatest(File fileImagesDir) {
        File[] files = fileImagesDir.listFiles();
        Arrays.sort(files, new Comparator<File>() {
                @Override
                public int compare(File lhs, File rhs) {
                    return Long.valueOf(rhs.lastModified()).compareTo(lhs.lastModified());
                }
            });

        return files;
    }

当我运行此代码时,sortFilesToLatest数组返回此

java.lang.NullPointerException: Attempt to get length of null array

我怎样才能解决这个问题?任何帮助或建议的人?

0 个答案:

没有答案