如何在android基础适配器中使用snackbar

时间:2016-07-18 07:16:32

标签: android adapter baseadapter

我在一个扩展到基本适配器的类中使用android小吃吧,当我点击图像时,我得到一个空指针异常。我使用此代码从sdcard中提取图像并将其显示在gridview中。

public class GridViewImageAdapter extends BaseAdapter{
private Activity _activity;
private ArrayList<String> _filePaths = new ArrayList<String>();
private int imageWidth;
private View view;
public GridViewImageAdapter(Activity activity, ArrayList<String> filePaths,
                            int imageWidth) {
    this._activity = activity;
    this._filePaths = filePaths;
    this.imageWidth = imageWidth;
}

@Override
public int getCount() {
    return this._filePaths.size();
}

@Override
public Object getItem(int position) {
    return this._filePaths.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}
public  void removeitem(int position){
    this._filePaths.remove(position);
    notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(_activity);
    } else {
        imageView = (ImageView) convertView;
    }

    // get screen dimensions
    Bitmap image = decodeFile(_filePaths.get(position), imageWidth,
            imageWidth);

    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
            imageWidth));
    imageView.setImageBitmap(image);

    // image view click listener
    imageView.setOnClickListener(new OnImageClickListener(position));

    return imageView;
}

class OnImageClickListener implements OnClickListener {

    int _postion;

    // constructor
    public OnImageClickListener(int position) {
        this._postion = position;
    }

    @Override
    public void onClick(View v) {
        // on selecting grid view image
        // launch full screen activity
      //  Intent i = new Intent(_activity, FullScreenViewActivity.class);
     //   i.putExtra("position", _postion);
     //   _activity.startActivity(i);

        final String imgPath=_filePaths.get(_postion);
        final Snackbar snackbar = Snackbar
                .make(view, "Delete Image?", Snackbar.LENGTH_LONG)
                .setAction("DELETE", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        File file = new File(imgPath);
                        file.delete();

                        Snackbar snackbar1 = Snackbar.make(view, "Image Deleted!", Snackbar.LENGTH_SHORT);
                        notifyDataSetChanged();
                        View sbView = snackbar1.getView();
                        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                        textView.setTextColor(Color.YELLOW);
                        snackbar1.show();

                    }
                });
        snackbar.setActionTextColor(Color.RED);
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();

    }

}

/*
 * Resizing image size
 */
public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
    try {

        File f = new File(filePath);

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        final int REQUIRED_WIDTH = WIDTH;
        final int REQUIRED_HIGHT = HIGHT;
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
            scale *= 2;

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

}

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
        at android.support.design.widget.Snackbar.<init>(Snackbar.java:180)
        at android.support.design.widget.Snackbar.make(Snackbar.java:209)
        at nidhinkumar.gridcam.camerasample.adapter.GridViewImageAdapter$OnImageClickListener.onClick(GridViewImageAdapter.java:95)
        at android.view.View.performClick(View.java:4848)
        at android.view.View$PerformClick.run(View.java:20260)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5624)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

2 个答案:

答案 0 :(得分:2)

问题在于您传递的视图Snackbar snackbar1 = Snackbar.make(view, "Image Deleted!", Snackbar.LENGTH_SHORT);您尚未初始化view。初始化view

答案 1 :(得分:0)

 public void onClick(View v) {
        // on selecting grid view image
        // launch full screen activity
      //  Intent i = new Intent(_activity, FullScreenViewActivity.class);
     //   i.putExtra("position", _postion);
     //   _activity.startActivity(i);
      view=v;
        final String imgPath=_filePaths.get(_postion);
        final Snackbar snackbar = Snackbar
                .make(view, "Delete Image?", Snackbar.LENGTH_LONG)
                .setAction("DELETE", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        File file = new File(imgPath);
                        file.delete();

                        Snackbar snackbar1 = Snackbar.make(view, "Image Deleted!", Snackbar.LENGTH_SHORT);
                        notifyDataSetChanged();
                        View sbView = snackbar1.getView();
                        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                        textView.setTextColor(Color.YELLOW);
                        snackbar1.show();

                    }
                });
        snackbar.setActionTextColor(Color.RED);
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();

    }

我已使用view = v andd初始化视图,现在它可以正常工作