尝试在getCount()中获取Null数组的长度;

时间:2017-04-25 17:24:55

标签: android android-fragments android-gridview

我正在使用片段来显示Gridview中特定文件夹中的图像... 但是在Lollipop&预棒棒糖,它工作正常,但在M或M +设备上,我正在崩溃与尝试获得Null阵列的长度'在我的适配器中的getCount()方法......

我不知道我哪里出错了。

这是Fragment Downloaded的代码 -

public class Downloaded extends Fragment{



    // Declare variables
    private String[] FilePathStrings;
    private String[] FileNameStrings;
    private File[] listFile;
    GridView grid;
    GridViewAdapter adapter;
    File file;
   TextView empty;

@Override
public void onActivityCreated( Bundle
savedInstanceState) {
super.onActivityCreated(savedInstanceState);
    getActivity().setTitle("Saved");
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        //Inflating the Layout 
        View rootView = inflater.inflate(R.layout.gridview_main, container, false); 


        // Check for SD Card
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                    .show();
        } else {
            // Locate the image folder in your SD Card
            file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "Sample");
            // 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];
            // Create a String array for FileNameStrings
            FileNameStrings = 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
                FileNameStrings[i] = listFile[i].getName();
            }
        }


        // Locate the GridView in gridview_main.xml
        grid = (GridView) rootView.findViewById(R.id.gridview);

       empty = (TextView) rootView.findViewById(R.id.empty);

       empty.setText("You haven't saved any Pic yet...!");

       grid.setEmptyView(empty);

        // Pass String arrays to LazyAdapter Class
        adapter = new GridViewAdapter(getActivity(), FilePathStrings, FileNameStrings);

        // Set the LazyAdapter to the GridView
        grid.setAdapter(adapter);



        // Capture gridview item click
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Intent i = new Intent(getActivity(), ViewImage.class);
                // Pass String arrays FilePathStrings
                i.putExtra("filepath", FilePathStrings);
                // Pass String arrays FileNameStrings
                i.putExtra("filename", FileNameStrings);
                // Pass click position
                i.putExtra("position", position);
                startActivity(i);
            }

        });
        return rootView;
    }

}

这是我的适配器 -

public class GridViewAdapter extends BaseAdapter {

    // Declare variables
    private Activity activity;
    private String[] filepath;
    private String[] filename;

    Context mContext;
    private static LayoutInflater inflater = null;

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
        activity = a;
        this.filepath = fpath;
        filename = fname;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return this.filepath.length;
     // Getting a Crash here!

    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;

        if (convertView == null)
            vi = inflater.inflate(R.layout.gridview_item, null);

        // Locate the ImageView in gridview_item.xml
        ImageView img = (ImageView) vi.findViewById(R.id.image);


        // Set file name to the TextView followed by the position
        TextView txt = (TextView) vi.findViewById(R.id.name);

        // Decode the filepath with BitmapFactory followed by the position


        // Set the decoded bitmap into ImageView
        Glide.with(activity)
        .load(filepath[position])
        .into(img);

       txt.setText(filename[position]);

        return vi;
    }
}

1 个答案:

答案 0 :(得分:1)

检查file.mkdirs()的返回值。它很可能会返回false,这会导致if (file.isDirectory())失败并且FilePathStrings没有被创建。

如果应用没有适当的权限来访问外部存储空间(例如WRITE_EXTERNAL_STORAGE),则可能会发生这种情况。如果定位M或更新,请确保您要求运行时权限;但是,请记住,即使您没有针对M,用户仍然可以撤销对#34;遗产的许可。没有使用运行时权限模型的样式应用程序。