如何使用通用图像加载器解决NullPointerException的错误?

时间:2015-10-28 04:55:33

标签: android universal-image-loader

我是一名初学者。我正在尝试使用通用图像加载器来显示样本库视图的图像,但它不起作用。我做错了什么?

感谢。

这是一个测试程序。

Adapater类:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import iop.demo_gallery.R;
import iop.demo_gallery.ViewImager;



public class DummyAdapter extends BaseAdapter {

    private Activity _activity;
    ImageLoader imageLoader;
    private String[] filepath;
    DisplayImageOptions options;
    private static LayoutInflater inflater = null;


    public DummyAdapter(  Activity activity,String[] filePaths)
    {
        this._activity = activity;
        this.filepath = filePaths;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return filepath.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.item_grid_image, parent, false);
            holder = new ViewHolder();
            assert view != null;
            holder.imageView = (ImageView) view.findViewById(R.id.image_view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        try {
            imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(filepath[position],holder.imageView, options); // NullPointerException here!
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        return view;
    }
    static class ViewHolder {
        ImageView imageView;
    }
}

主要课程:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.util.ArrayList;

import iop.demo_gallery.adapter.CustomPagerAdapter;
import iop.demo_gallery.adapter.DummyAdapter;


public class Gallery_view extends ActionBarActivity
{

    private String[] FilePathStrings;
    private String[] FileNameStrings;
    private File[] listFile;
    ImageLoader imageLoader;
    DisplayImageOptions options;
    DummyAdapter dummyAdapter;
    File file;
    GridView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_view);


        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(Gallery_view.this));

        options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.ic_error)
        .showImageForEmptyUri(R.drawable.ic_empty)
        .cacheOnDisc()
        .cacheInMemory()
        .build();


        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
        } else {
            // Locate the image folder in your SD Card
            file = new File( "file:///"+Environment.getExternalStorageDirectory().toString()+ File.separator + "/Flipkart");
            // Create a new folder if no folder named SDImageTutorial exist

            file.mkdirs();
        }

        if (file.isDirectory())
        {
            listFile = file.listFiles();
            // Create a String array for FilePathStrings
            FilePathStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++)
            {
                // Get the path of the image file
                FilePathStrings[i] = listFile[i].getAbsolutePath();
                // Get the name image file
            }
        }

        dummyAdapter = new DummyAdapter(this,FilePathStrings);
        listView = (GridView)findViewById(R.id.grid_gallery);
        listView.setAdapter(dummyAdapter);
        dummyAdapter.notifyDataSetChanged();
    }
}

日志

10-28 10:06:55.343 11715-11715/iop.demo_gallery E/AndroidRuntime:Caused    by: java.lang.NullPointerException: Attempt to get length of null array
10-28 10:06:55.343 11715-11715/iop.demo_gallery E/AndroidRuntime:     at iop.demo_gallery.adapter.DummyAdapter.getCount(DummyAdapter.java:51)
10-28 10:06:55.343 11715-11715/iop.demo_gallery E/AndroidRuntime:     at android.widget.GridView.setAdapter(GridView.java:201)
10-28 10:06:55.343 11715-11715/iop.demo_gallery E/AndroidRuntime:     at iop.demo_gallery.Gallery_view.onCreate(Gallery_view.java:93)

1 个答案:

答案 0 :(得分:2)

您在FilePathStrings内创建了if (file.isDirectory())个对象 如果file.isDirectory()为false,则FilePathStrings为空,因此如果条件

则移动代码
 if (file.isDirectory())
    {
        listFile = file.listFiles();
        // Create a String array for FilePathStrings
        FilePathStrings = new String[listFile.length];

        for (int i = 0; i < listFile.length; i++)
        {
            // Get the path of the image file
            FilePathStrings[i] = listFile[i].getAbsolutePath();
            // Get the name image file
        }
        dummyAdapter = new DummyAdapter(this,FilePathStrings);
        listView = (GridView)findViewById(R.id.grid_gallery);
        listView.setAdapter(dummyAdapter);
        dummyAdapter.notifyDataSetChanged();
    }

else

之后添加if (file.isDirectory())条件
   else{
        FilePathStrings = new String[0];
    }
    dummyAdapter = new DummyAdapter(this,FilePathStrings);
    listView = (GridView)findViewById(R.id.grid_gallery);
    listView.setAdapter(dummyAdapter);
    dummyAdapter.notifyDataSetChanged();