web-dev-qa-db-ja.com

NullPointerException:仮想メソッド 'int Android.graphics.Bitmap.getWidth()'を呼び出そうとしました

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.getBitmap(CategoryAdapter.Java:92)のcom.adapter.CategoryAdapterのnullオブジェクト参照で仮想メソッド 'int Android.graphics.Bitmap.getWidth()'を呼び出そうとしました。 getView(CategoryAdapter.Java:75)at Android.widget.AbsListView.obtainView(AbsListView.Java:2896)at Android.widget.GridView.makeAndAddView(GridView.Java:1456)at Android.widget.GridView.makeRow(GridView.Java :361)Android.widget.GridView.fillDown(GridView.Java:302)at Android.widget.GridView.fillFromTop(GridView.Java:437)at Android.widget.GridView.layoutChildren(GridView.Java:1284)at Android .widget.AbsListView.onLayout(AbsListView.Java:2700)at Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android.widget.RelativeLayout。 onLayout(RelativeLayout.Java:1077)at Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android.widget.FrameLayout.layoutChildren(FrameLayo ut.Java:579)Android.widget.FrameLayout.onLayout(FrameLayout.Java:514)at Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405 )Android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.Java:1043)at Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405) Android.widget.LinearLayout.setChildFrame(LinearLayout.Java:1702)at Android.widget.LinearLayout.layoutVertical(LinearLayout.Java:1556)at Android.widget.LinearLayout.onLayout(LinearLayout.Java:1465)at Android.view。 View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android.widget.RelativeLayout.onLayout(RelativeLayout.Java:1077)at Android.view.View.layout(View .Java:16899)Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:579)at Android.widget.FrameLayout.onLayout(FrameLayout.Java:514) AndroidのAndroid.view.View.layout(View.Java:16899)で。 Android.widget.LinearLayout.layoutVertical(LinearLayout.Java:1556)at Android.widget.LinearLayout.onLayoutのAndroid.widget.LinearLayout.setChildFrame(LinearLayout.Java:1702)でのview.ViewGroup.layout(ViewGroup.Java:5405) (LinearLayout.Java:1465)at Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java: 579)Android.widget.FrameLayout.onLayout(FrameLayout.Java:514)at Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android。 Android.view.View.layoutのAndroid.widget.LinearLayout.layoutVertical(LinearLayout.Java:1556)のAndroid.widget.LinearLayout.onLayout(LinearLayout.Java:1465)のwidget.LinearLayout.setChildFrame(LinearLayout.Java:1702) (View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:579)at Android.widget.FrameLayout.onLayout(FrameLayout.Java: 514 )Android.view.View.layout(View.Java:16899)at Android.view.ViewGroup.layout(ViewGroup.Java:5405)

どうすればこれを解決できますか?

16
Bakya

エラートレースは、エラーが発生することを示しています

_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: ..._のようなログに注意して、このメソッドが失敗した理由を確認してください。多分あなたは間違ったパスを提供しました。

12
Carlos Parker

私の場合の問題は、nullを返していたバックグラウンドスレッドで「decodeFile」を呼び出すことでした。そのコードをメインスレッドに移動すると、問題が解決しました。

0
Shah