nullポインタの例外は何か知っていますが、私はそれを処理する方法を理解することができず、ユーザーにリストを伝えるトーストメッセージを出していません次のコードではnullです。私は自分のアプリケーション用の画像ギャラリーを作成しており、システムがすべてのファイルとフォルダをAndroidアプリケーションにロードするパスを指定しています。長さの配列をチェックする場所を特定できない
パスにファイルやフォルダが存在しない場合は、アプリがクラッシュしたと言って:
Attempting to invoke length on a null array.
GalleryFragment.javaが
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
ImageView icon = (ImageView) getActivity().findViewById(R.id.toolbarIcon);
icon.setImageResource(R.drawable.ic_perm_media_black_24dp);
icon.setColorFilter(getResources().getColor(R.color.Gallery));
TextView title = (TextView) getActivity().findViewById(R.id.toolbarTitle);
title.setText(getString(R.string.galleryLabel));
toolbar.setBackground(getResources().getDrawable(R.drawable.tile_green));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_menu_green_24dp));
gridView = (GridView) view.findViewById(R.id.gridView);
setGridAdapter(Environment.getExternalStorageDirectory().getPath());
return view;
}
private void setGridAdapter(String path) {
// Create a new grid adapter
gridItems = createGridItems(path);
MyGridAdapter adapter = new MyGridAdapter(getContext(), gridItems);
// Set the grid adapter
gridView.setAdapter(adapter);
// Set the onClickListener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (gridItems.get(position).isDirectory()) {
setGridAdapter(gridItems.get(position).getPath());
}
else {
gridItems.get(position).getImage();
}
}
});
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (gridItems.get(position).isDirectory()) {
Dialog dialog = new Dialog(getContext());
dialog.setTitle("Test");
dialog.setContentView(R.layout.activity_recognition);
dialog.show();
return false;
} else {
Dialog dialog = new Dialog(getContext());
dialog.setTitle("Test");
dialog.setContentView(R.layout.activity_reset_password);
dialog.show();
return false;
}
}
});
}
private List<GridViewItem> createGridItems(String directoryPath) {
List<GridViewItem> items = new ArrayList<>();
// List all the items within the folder.
File[] files = new File(directoryPath).listFiles(new ImageFileFilter());
for (File file : files) {
// Add the directories containing images or sub-directories
if (file.isDirectory()
&& file.listFiles(new ImageFileFilter()).length > 0) {
items.add(new GridViewItem(file.getAbsolutePath(), true, null));
}
// Add the images
else {
Bitmap image = BitmapHelper.decodeBitmapFromFile(file.getAbsolutePath(),
50,
50);
items.add(new GridViewItem(file.getAbsolutePath(), false, image));
}
}
return items;
}
/**
* Checks the file to see if it has a compatible extension.
*/
private boolean isImageFile(String filePath) {
if (filePath.endsWith(".jpg") || filePath.endsWith(".png"))
// Add other formats as desired
{
return true;
}
return false;
}
/**
* This can be used to filter files.
*/
private class ImageFileFilter implements FileFilter {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
else if (isImageFile(file.getAbsolutePath())) {
return true;
}
return false;
}
}
GalleryAdapter.java
public class MyGridAdapter extends BaseAdapter {
LayoutInflater inflater;
List <GridViewItem> items;
private Context mContext;
public MyGridAdapter(Context context, List <GridViewItem> items) {
mContext = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(items.get(position).getPath());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
Bitmap image = items.get(position).getImage();
if (image != null) {
imageView.setImageBitmap(image);
} else {
// If no image is provided, display a folder icon.
imageView.setImageResource(R.drawable.ic_folder_grey_24dp);
}
return convertView;
}
}
配列の長さが0より大きい場合にのみ、リストファイルを作成します。私はどこをチェックするのか分からない。