-1

From the logcat I understand that the Activity crashes because of NullPointerException causes by this line of code.

mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), 
                                   R.id.picture, imageWidth, imageHeight));

I have read articles about similar problem but still getting NullPointerException. I think that mistake right under my nose, but still I can't catch it.

Where should I put this line to avoid NullPointerException?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
...
    ImageView mImageView = (ImageView) findViewById(R.id.picture);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.picture, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;

    mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), 
                               R.id.picture, imageWidth, imageHeight));
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

and here is ImageView from layout.

<ImageView
        android:layout_height="240dp"
        android:layout_width="199dp"
        android:src="@drawable/galaxy"
        android:id="@+id/picture"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitCenter"
        android:layout_weight="0.19" />
Robert
  • 35
  • 8
  • In addition to editing your question to provide the full stack trace, please replace the `...` in `onCreate()` with the actual code, since your problem may well lie in there. – CommonsWare Mar 24 '16 at 16:36
  • This line is parent line of error, you have more nested methods there, So error in that line means that error may be in anywhere between those nested methods. Be specific, There may be another line below in stacktrace. – Shree Krishna Mar 24 '16 at 16:43
  • Thanks for trying to help me. I noticed that actually each method I try to call for mImageView() gives me NullPointerException, even native android methods like `mImageView.setImageResource(R.drawable.newpic);` All of the code is written in PreferenceActivity. Maybe that is a key? – Robert Mar 25 '16 at 20:22

1 Answers1

0

There is a cleaner alternative to display a bitmap onto a imageview. Try this:

private Bitmap bitmap_image; 

Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//get image from resources
bitmap_image = BitmapFactory.decodeResource(getResources(),   R.drawable.picture);
ImageView mImageView = (ImageView) findViewById(R.id.picture);
mImageView.setImageBitmap(bitmap_image);
}

-To retrieve a image from resources and store it into a bitmap, use BitmapFactory.decodeResource(getResources(), R.drawable.yourimagename);

-at this point you can directly set the image to a imageView. If you want to manipulate the size of the image here is a function that will resize a bitmap:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

        int width = bm.getWidth();

        int height = bm.getHeight();

        float scaleWidth = ((float) newWidth) / width;

        float scaleHeight = ((float) newHeight) / height;

        // CREATE A MATRIX FOR THE MANIPULATION

        Matrix matrix = new Matrix();

        // RESIZE THE BIT MAP

        matrix.postScale(scaleWidth, scaleHeight);

        // RECREATE THE NEW BITMAP

        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

        return resizedBitmap;

    }

-If you need clarification on the re-sizing of the bitmap, refer to this stackoverflow post:

How to Resize a Bitmap in Android?

-if you need a full working example, i have a activity that utilizes retrieving a image from resources and storing it into a bitmap and then displaying it on screen. The only part I do not have that you request, is displaying the bitmap in a imageView.

Github class link: https://github.com/Rifat-Rashid/thePlatformer/blob/master/MainActivity.java

Community
  • 1
  • 1
  • Thanks for trying to help me. I noticed that actually each method I try to call for mImageView() gives me NullPointerException, even native android methods like `mImageView.setImageResource(R.drawable.newpic);` All of the code is written in PreferenceActivity. Maybe that is a key? – Robert Mar 25 '16 at 20:25
  • Possibly. Im not too sure. Do you have github? Inspecting the code would make this a lot easier! – Rifat Rashid Mar 27 '16 at 19:09