0

I've a fragment where I need to select some Images from Gallery. So basically I want to display a dialog where I populate with thumbnail images and checkbox. Images and checkboxes are correctly displayed, but when I select one or more checkbox it returns a "JavaNullPointerException" on a null object. This code working fine if I use an activity, but with fragment I don't understand where is the mistake. Below my code:

My method to call the dialog.

private void scegliFotoDaMostrare(){
    final Dialog d = new Dialog(getActivity());
    d.setContentView(R.layout.activity_show_image);

    ImageList = getSD();

    // gridView1
     gView1 = (GridView)d.findViewById(R.id.gridView1);

    gView1.setAdapter(new ImageAdapter(getActivity(),ImageList));


    // Check All
    Button btnCheckAll = (Button) d.findViewById(R.id.btnCheckAll);
    btnCheckAll.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                checkbox.setChecked(true);
            }
        }
    });

    // Clear All
    Button btnClearAll = (Button) d.findViewById(R.id.btnClearAll);
    btnClearAll.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                checkbox.setChecked(false);
            }
        }
    });

    // Get Item Checked
    Button btnGetItem = (Button) d.findViewById(R.id.btnGetItem);
    btnGetItem.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                if(checkbox.isChecked())
                {
                    Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());

                    Toast.makeText(getActivity(),checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();

                }
            }
        }
    });
    d.show();

    }

The Adapter

public class ImageAdapter extends BaseAdapter{        
    private Context context;
    private List <String> lis;

    public ImageAdapter(Context c, List <String> li)
    {
        // TODO Auto-generated method stub
        context = c;
        lis = li;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return lis.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.showimage, null);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.textView1);
        String strPath = lis.get(position).toString();

        // Get File Name
        String fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
        textView.setText(fileName);

        // Image Resource
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        Bitmap bm = BitmapFactory.decodeFile(strPath);
        imageView.setImageBitmap(bm);

        // CheckBox
        CheckBox Chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        Chkbox.setTag(fileName);

        return convertView;

    }
}

Get the List !

    private List <String> getSD() {
     List <String> it = new ArrayList <String>();
     String pathImmagini =  Environment.getExternalStorageDirectory().toString() +
     File.separator + "DCIM"+ File.separator +  File.separator;
     File f = new File(pathImmagini);
     File files[] = f.listFiles();
     String titoloDaCercare = "giretto";      
     for (int i = 0; i <files.length; i++){   
              String fileName = (files[i].getName().substring(0,files[i].getName().indexOf(";"))
                .replace("IMG_",""));
         if (fileName.equals( titoloDaCercare) ){
              File file = files[i];
              Log.d("Count", file.getPath());
              it.add(file.getPath());
         }
     }
    return it;
}
Magobin
  • 93
  • 2
  • 13
  • 2
    Did you debug it? Which specific method/line is returning null? – miguelarc Jul 19 '18 at 14:28
  • yes, the resource that returning null is CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1); checkbox.setChecked(true); – Magobin Jul 19 '18 at 15:07
  • if, for example check the third checkbox, at 4 t returns null...I saw that it doesn't return the exact checkbox....if I check the first I saw that six is checked too. – Magobin Jul 19 '18 at 15:11
  • After hours, I don't understand why when I select a checkbox, other checkbox are selected at the same time. Basically I'm looking for an AlertDialog that display some images from SD card and record which are selected. Any suggesion ? – Magobin Jul 20 '18 at 13:45
  • Try launching an intent for the gallery, it's easier than using a dialog and having the images displayed there. In the gallery image picker you can select multiple images. If you want to opt for 3rd party libraries, try checking [this link](https://stackoverflow.com/questions/43729795/multiple-image-select-from-gallery). – miguelarc Jul 20 '18 at 13:47
  • thanks for link...unfortunately I need to show only images from my app (that are geotagged). I wonder if it's possible to choose the folder where pickup the images – Magobin Jul 20 '18 at 15:06
  • You can specify the uri of the folder that you want to open and setDataAndType() for the selected uri. Check [this link](https://stackoverflow.com/questions/17165972/android-how-to-open-a-specific-folder-via-intent-and-show-its-content-in-a-file). Try changing the "resource/folder" to "images/*" and see if it works. – miguelarc Jul 20 '18 at 15:09
  • intent = new Intent(Intent.ACTION_GET_CONTENT);Uri uri = Uri.parse(Environment.getExternalStorageDirectory().toString()+ File.separator + "DCIM"+ File.separator + "MyApp"+ File.separator); intent.setDataAndType(uri, "image/*"); startActivity(Intent.createChooser(intent, "Open folder")); ...but in this way it open only recent images. Is possible to do a cursor on intent pickup image to customize where to find images ? – Magobin Jul 20 '18 at 15:39

0 Answers0