NullPointerException at createBitmap(). Using a ListView to inflate a Bitmap into a row



I'm getting a NullPointerException at createBitmap. On research, I've come across people suggesting it's because of low memory. I'd like to find out if there's anything in my code that could be changed to make it work. For reference, I'll also post the XML file of the row to give an idea of how it should fit in.


Here's the function that gives the error (I'm trying to make the image circular, with this function):



public Bitmap getCroppedBitmap(Bitmap bitmap, View view) {
Bitmap output;
try {
view.buildDrawingCache(true);
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), //Error here
Bitmap.Config.ARGB_8888);
}finally {
view.setDrawingCacheEnabled(false);
}

final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

Canvas canvas = new Canvas(output);

final Paint paint = new Paint();
paint.setAntiAlias(true);

int halfWidth = bitmap.getWidth()/2;
int halfHeight = bitmap.getHeight()/2;

canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}


Here's the XML code for the row:



<android.support.v7.widget.CardView xmlns:android="http://ift.tt/nIICcg"
xmlns:cardview="http://ift.tt/GEGVYd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal"
android:stateListAnimator="@anim/anim"
cardview:cardBackgroundColor="@color/material_black_900"
cardview:cardElevation="5dp"
cardview:cardCornerRadius="10dp">

<RelativeLayout
android:id="@+id/item_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:scaleType="center"/>

<TextView
android:id="@+id/name_view"
android:paddingTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/WhiteSmoke"
android:layout_centerHorizontal="true"
android:textSize="15dp"
android:singleLine="false"
android:text="Test1"
android:paddingBottom="10dp"
android:textStyle="bold"/>

<TextView
android:id="@+id/number_view"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:paddingBottom="5dp"
android:singleLine="false"
android:layout_height="wrap_content"
android:layout_below="@+id/name_view"
android:text="Test2"
android:textColor="@color/WhiteSmoke"
android:textSize="15dp"
android:textStyle="bold"/>

</RelativeLayout>

No comments:

Post a Comment