在Android中按下按钮时出现NullPointer异常

时间:2015-11-20 04:35:11

标签: android

我有图像的网格视图,我选择了多个图像,并在主要活动中的按钮点击事件中删除了所选图像。但是没有选择任何图像按下按钮获取空指针异常并且应用程序崩溃。如何解决这个问题。这是我的按钮代码。提前谢谢。

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;
        }
    }

此行错误

ArrayList<String> selectedItems = gridAdapter.getCheckedItems();

1 个答案:

答案 0 :(得分:1)

您的gridAdapter为空

将此行作为onClick方法中的第一行

GridView_Adapter gridAdapter = new GridView_Adapter();