首页 文章

NullPointerException:尝试调用虚方法'int android.graphics.Bitmap.getWidth()'

提问于
浏览
6
public class CategoryAdapter extends BaseAdapter {
Context context;
ArrayList<ModelCategory> model;
LayoutInflater layoutInflater;

public CategoryAdapter(Activity activity, ArrayList<ModelCategory> model) {
    this.model = model;
    this.context = activity;
}

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

@Override
public Object getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.category_adapter, parent, false);
        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.category_grid_image);
        viewHolder.textView = (TextView) convertView.findViewById(R.id.get_category_title);
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.textView.setText(model.get(position).getCategoryName());
    if(position<=3){
        viewHolder.imageView.setImageBitmap(StringToBitMap(model.get(position).getCategoryImage()));
    }
    else {
        viewHolder.imageView.setImageBitmap(getBitmap(model.get(position).getCategoryImage()));
    }
    return convertView;
}

private class ViewHolder {
    public ImageView imageView;
    public TextView textView;
}

private Bitmap getBitmap(String path) {

    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 8;
    Bitmap bitmap = BitmapFactory.decodeFile(path, option);
    Matrix matrix = new Matrix();
    matrix.postRotate(getImageOrientation(path));
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    Bitmap resized = Bitmap.createScaledBitmap(rotatedBitmap, 150, 150, true);
    return resized;
}


private static int getImageOrientation(String imagePath) {
    int rotate = 0;
    try {
        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(
                imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return rotate;
}


public Bitmap StringToBitMap(String encodedString){
    try {
        byte [] encodeByte=Base64.decode(encodedString, Base64.DEFAULT);
        Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        return bitmap;
    } catch(Exception e) {
        e.getMessage();
        return null;
    }
}

}

我得到 error 如下

java.lang.NullPointerException:尝试在com.adapter.CategoryAdapter的com.adapter.CategoryAdapter.getBitmap(CategoryAdapter.java:92)上的空对象引用上调用虚拟方法'int android.graphics.Bitmap.getWidth()' .getView(CategoryAdapter.java:75)在android.widget.AbsListView.obtainView(AbsListView.java:2896)的android.widget.GridView.makeAndAddView(GridView.java:1456)android.widget.GridView.makeRow(GridView . java:361)在android.widget.GridView.fillDown(GridView.java:302)的android.widget.GridView.fillFromTop(GridView.java:437)在android.widget.GridView.layoutChildren(GridView.java:1284)at android.widget.AbsListView.onLayout(AbsListView.java:2700)位于android.view.Viewout上的android.view.View.layout(View.java:16899),android.widget . .onLayout(RelativeLayout.java:1077)在android.view.Viewout上的android.view.View.layout(View.java:16899),android.widget.FrameLayout.layoutChildren(FrameLa)上的android.view.Viewout.layout(ViewGroup.java:5405) yout.java:579)在Android.view.joutout(ViewLayout.java:514)android.view.View.layout(View.java:16899)android.view.ViewGroup.layout(ViewGroup.java:5405)的android.widget.FrameLayout.onLayout(FrameLayout.java:514) )在android.view.View.layout(ViewGroup.java:5405)的android.view.View.layout(View.java:16899)的android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)的android.widget.LinearLayout.onoutVertical(LinearLayout.java:1556)android.view上的android.widget.LinearLayout.onLayout(LinearLayout.java:1465) . 在android.view.View.layout(视图)的android.view.View.Viewout.Loutout(ViewLayout.java:1077)的android.view.View.Viewout(View.java:16899)上的View.layout(View.java:16899) .java:16899)在Android.widget.FrameLayout.onLayout(FrameLayout.java:514)的android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)的android.view.ViewGroup.layout(ViewGroup.java:5405)在androi的android.view.View.layout(View.java:16899)在Android.widget.LineoutLayout的android.widget.LinearLayout.setoutVertical(LinearLayout.java:1556)的android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)的d.view.ViewGroup.layout(ViewGroup.java:5405) .onLayout(LinearLayout.java:1465)位于android.view.Viewout上的android.view.View.layout(View.java:16899),android.widget.FrameLayout.layoutChildren(FrameLayout . java:579)在android.view.View.Loutout(FrameLayout.java:514)的android.view.View.layout(View.java:16899)android.view.ViewGroup.layout(ViewGroup.java:5405)at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)位于android.view.View.View的android.widget.LinearLayout.onLayout(LinearLayout.java:1465)android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)在Android.widget.FrameLayout.onLayout(FrameLayout . )的android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)的android.view.ViewGroup.layout(ViewGroup.java:5405)处的.layout(View.java:16899) . java的:5 14)在android.view.View.layout(View.java:16899)处于android.view.ViewGroup.layout(ViewGroup.java:5405)

我怎么解决这个问题?

2 回答

  • 8

    错误跟踪显示错误发生

    Bitmap bitmap = BitmapFactory.decodeFile(path, option);
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    

    作为 public static Bitmap decodeFile(String pathName, Options opts) 方法的文档:

    return The decoded bitmap, or null if the image data could not be
     decoded, or, if opts is non-null, if opts requested only the
     size be returned (in opts.outWidth and opts.outHeight)
    

    如果无法解码图像数据,则此方法将返回null . 注意像 E/BitmapFactory: Unable to decode stream: ... 这样的日志来找出这个方法失败的原因 . 也许你提供了错误的道路 .

  • 0

    我的问题是我在后台线程中调用'decodeFile'而返回null . 将该代码移动到主线程修复了问题 .

相关问题