-1
我正在使用一個片段,顯示從GridView中的特定文件夾的圖像... 但是在棒棒糖&前棒棒糖,它工作正常,但在M或M +設備上,我得到一個崩潰與'嘗試獲取在我利用getCount()方法適配器空陣」的長度...嘗試獲取getCount()中的空數組的長度;
我不知道我要去的地方錯了..
這裏是我的片段下載的代碼 -
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;
}
}