2

I have grid-view with images and i select the multiple images and delete selected images on button click event in Main Activity .But without select any images pressed the button getting the null-pointer exception and application was crash.How to solve this issue.Here is my button code.Thanks in advance.

imgDeleteImagesFromGallery = (ImageView) findViewById(R.id.imgDeleteImage);
imgDeleteImagesFromGallery.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {
        ArrayList<String> selectedItems = gridAdapter.getCheckedItems();
        if(selectedItems.size() > 0)
        {
            Toast.makeText(AddPost.this, "Total photos selected: "+selectedItems.size(), Toast.LENGTH_SHORT).show();
            Log.e(AddPost.class.getSimpleName(), "Selected Items: " + selectedItems.toString());

            Iterator it = picsName.iterator();
            while (it.hasNext())
            {
                strFilePath = it.next().toString();
                Log.e("strFilePath ", " strFilePath = " + strFilePath);
                File file = new File(strFilePath);
                if (file.exists())
                {
                    boolean result = file.delete();
                    Log.e("File deleted ", " From classnkk_images = " + result);
                }
            }

            gridAdapter.notifyDataSetChanged();
            int countImg = gridAdapter.getCount();
            textTotalImages.setText(Integer.toString(countImg));
        }

    }
});





class GridView_Adapter extends BaseAdapter {

        private LayoutInflater mInflater;
        SparseBooleanArray mSparseBooleanArray;
        public GridView_Adapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mSparseBooleanArray = new SparseBooleanArray();
        }

        public ArrayList<String> getCheckedItems() {
            ArrayList<String> mTempArry = new ArrayList<String>();

            for(int i=0;i<insertImageAudioList.size();i++) {
                if(mSparseBooleanArray.get(i)) {
                    mTempArry.add(insertImageAudioList.get(i));
                }}
            return mTempArry;
        }

        public int getCount() {
            return insertImageAudioList.size();
        }

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            final ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.add_post_grid_item_layout, parent , false);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            String strImagePosition = insertImageAudioList.get(position);
            if (strImagePosition.endsWith(mp3_Pattern))
            {
                Log.e("In if condition  ","strImagePosition = "+strImagePosition);
                holder.image.setImageResource(R.drawable.resize_audio_control);

            }
            if (strImagePosition.endsWith(png_Pattern) || strImagePosition.endsWith(jpg_pattern) || strImagePosition.endsWith(bmp_pattern) || strImagePosition.endsWith(gif_pattern) || strImagePosition.endsWith(jpeg_pattern))
            {
                //Log.e("strImagePosition "," = "+strImagePosition);
                String strBaseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                String filepath = strBaseDir + "/classnkk_images/" + strImagePosition;
                File file = new File(filepath);

                    fileName.add(file.getAbsolutePath());
                    Bitmap myBitmap = BitmapFactory.decodeFile(filepath);
                    holder.image.setImageBitmap(myBitmap);
            }


            holder.checkbox.setTag(position);


            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
                    int pos = (Integer) buttonView.getTag();
                    if (!buttonView.isChecked())
                    {
                        picsName.remove((String) insertImageAudioList.get(pos));
                    }
                    else if (buttonView.isChecked())
                    {
                        if (!picsName.contains((String) insertImageAudioList.get(pos)))
                        {
                            picsName.add((String) insertImageAudioList.get(pos));
                        }
                    }
                    notifyDataSetChanged();

                }
            });


            return convertView;
        }

        class ViewHolder
        {
            ImageView image;
            CheckBox checkbox;
            int id;
        }
    }

Error at this line

ArrayList<String> selectedItems = gridAdapter.getCheckedItems();
9
  • 1
    Means gridAdapter is null. please show more code where creating gridAdapter object of Class Commented Nov 20, 2015 at 4:37
  • possibility of having insertImageAudioList as null Commented Nov 20, 2015 at 4:50
  • That's why I'm asking when i press the delete button without select any images , the app crash .So how to handle it . Commented Nov 20, 2015 at 4:56
  • put this line ArrayList<String> selectedItems = gridAdapter.getCheckedItems(); above button onclicklistener and check if(selectedItems>0){ put onClicklistener here} Commented Nov 20, 2015 at 5:02
  • remove ArrayList<String> selectedItems = gridAdapter.getCheckedItems(); from inside button onClick Commented Nov 20, 2015 at 5:03

1 Answer 1

1

Your gridAdapter is null

put this line as first line inside your onClick method

GridView_Adapter gridAdapter = new GridView_Adapter();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.