NullPointerException:尝试调用虚拟方法';intandroid.graphics.Bitmap.getWidth()'; 公共类CategoryAdapter扩展了BaseAdapter{ 语境; ArrayList模型; LayoutInflater LayoutInflater; 公共类别适配器(活动,ArrayList模型){ this.model=模型; 这个上下文=活动; } @凌驾 public int getCount(){ 返回model.size(); } @凌驾 公共对象getItem(int位置){ 返回null; } @凌驾 公共长getItemId(int位置){ 返回位置; } @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ ViewHolder ViewHolder=null; layoutInflater=(layoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE); if(convertView==null){ viewHolder=新的viewHolder(); convertView=LayoutFlater.充气(R.layout.category_适配器,父级,false); viewHolder.imageView=(imageView)convertView.findViewById(R.id.category\u grid\u image); viewHolder.textView=(textView)convertView.findViewById(R.id.get\u category\u title); convertView.setTag(viewHolder); }否则{ viewHolder=(viewHolder)convertView.getTag(); } viewHolder.textView.setText(model.get(position.getCategoryName()); 如果(位置

NullPointerException:尝试调用虚拟方法';intandroid.graphics.Bitmap.getWidth()'; 公共类CategoryAdapter扩展了BaseAdapter{ 语境; ArrayList模型; LayoutInflater LayoutInflater; 公共类别适配器(活动,ArrayList模型){ this.model=模型; 这个上下文=活动; } @凌驾 public int getCount(){ 返回model.size(); } @凌驾 公共对象getItem(int位置){ 返回null; } @凌驾 公共长getItemId(int位置){ 返回位置; } @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ ViewHolder ViewHolder=null; layoutInflater=(layoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE); if(convertView==null){ viewHolder=新的viewHolder(); convertView=LayoutFlater.充气(R.layout.category_适配器,父级,false); viewHolder.imageView=(imageView)convertView.findViewById(R.id.category\u grid\u image); viewHolder.textView=(textView)convertView.findViewById(R.id.get\u category\u title); convertView.setTag(viewHolder); }否则{ viewHolder=(viewHolder)convertView.getTag(); } viewHolder.textView.setText(model.get(position.getCategoryName()); 如果(位置,android,exception,bitmap,Android,Exception,Bitmap,错误跟踪显示错误发生在 public class CategoryAdapter extends BaseAdapter { Context context; ArrayList<ModelCategory> model; LayoutInflater layoutInflater; public CategoryAdapter(Activity activity, ArrayList<ModelCategory> model) { this.model = m

错误跟踪显示错误发生在

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;
    }
}
作为
公共静态位图解码文件(字符串路径名,选项选项)
方法的文档:

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

如果图像数据无法解码,此方法将返回null。请注意类似
E/BitmapFactory:无法解码流:…
的日志,以找出此方法失败的原因。可能是您提供了错误的路径。

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

您的位图创建似乎失败。是否确定
路径
值正确并指向实际位图?再次检查getBitmap方法,您将获得空指针。
 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)