尝试显示位图时出现NullPointerException

时间:2016-03-24 16:33:25

标签: android nullpointerexception android-imageview android-bitmap bitmapfactory

从logcat我知道由于这行代码导致NullPointerException导致Activity崩溃。

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

我已阅读有关类似问题但仍会收到NullPointerException的文章。我认为这个错误在我的鼻子底下,但我仍然无法抓住它。

我应该把这一行放在哪里以避免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;
}

这里是来自布局的ImageView。

<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" />

1 个答案:

答案 0 :(得分:0)

有一种更清晰的替代方法可以在imageview上显示位图。 试试这个:

void reset() {
  ILoggerFactory factory = LoggerFactory.getILoggerFactory();
  LoggerContext context = (LoggerContext)factory;
  context.reset();
  ContextInitializer initializer = new ContextInitializer(context);
  initializer.autoConfig();
}

- 要从资源中检索图像并将其存储到位图中,请使用BitmapFactory.decodeResource(getResources(),R.drawable.yourimagename);

- 此时您可以直接将图像设置为imageView。如果你想操纵图像的大小,这里有一个调整位图大小的函数:

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);
}

- 如果您需要澄清重新调整位图的大小,请参阅此stackoverflow文章:

How to Resize a Bitmap in Android?

- 如果您需要一个完整的工作示例,我有一个活动,它利用从资源中检索图像并将其存储到位图中,然后在屏幕上显示它。我没有你要求的唯一部分是在imageView中显示位图。

Github类链接: https://github.com/Rifat-Rashid/thePlatformer/blob/master/MainActivity.java