【问题标题】:NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()'NullPointerException:尝试调用虚拟方法“int android.graphics.Bitmap.getWidth()”
【发布时间】:2016-01-25 09:38:02
【问题描述】:
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:尝试调用虚拟方法'int android.graphics.Bitmap.getWidth()' 在空对象引用上 在 com.adapter.CategoryAdapter.getBitmap(CategoryAdapter.java:92) 在 com.adapter.CategoryAdapter.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) 在 android.widget.AbsListView.onLayout(AbsListView.java:2700) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1465) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1465) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1465) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:16899) 在 android.view.ViewGroup.layout(ViewGroup.java:5405)

我该如何解决这个问题?

【问题讨论】:

  • 您的位图创建似乎失败了。您确定 path 值正确并指向实际位图吗?
  • 再次检查 getBitmap 方法,你得到的是空指针。

标签: android exception bitmap


【解决方案1】:

错误跟踪显示错误发生在

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: ... 之类的日志,以找出此方法失败的原因。也许您提供了错误的路径。

【讨论】:

    【解决方案2】:

    我的问题是我在返回 null 的后台线程中调用了“decodeFile”。将该代码移至主线程解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 2018-01-07
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多