Williams Tobi Williams Tobi - 1 month ago 18
Java Question

Bitmap not Showing in Dialog

I have an issue with setting a bitmap in an Alert dialog. I created a barcode in another class and I want to show the barcode in a dialog. It keeps showing an error. From the logs, the barcode is being created successfully. The problem keeps pointing to the

qrCodeImageView.setImageBitmap(bitmap);
line. Please help.

This is the method in the activity.

public void showQRCode(final Bundle bundle){
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
barcodeUtil = new BarcodeUtil();

Bitmap bitmap = barcodeUtil.createQRCcode(ContactDetailActivity.this, bundle);

AlertDialog.Builder builder = new AlertDialog.Builder(ContactDetailActivity.this);
Log.i(Constants.TAG_GBACARD, "Bmp in Dialog: " + bitmap);

LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.custom_dialog, null);
builder.setView(dialogLayout);


final ImageView qrCodeImageView = (ImageView) findViewById(R.id.qrCode);



qrCodeImageView.setImageBitmap(bitmap);

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

AlertDialog b = builder.create();
b.show();

} catch (Exception e){
e.printStackTrace();
}
}
});
}


This is the method that creates the barcode

public Bitmap createQRCcode(final Context context, final Bundle bundle) {
Bitmap bitmap = null;
try {
//Find screen size
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
int height = point.y;
int smallerDimension = width < height ? width : height;
smallerDimension = smallerDimension * 3 / 4;

//Encode with a QR Code image
qrCodeEncoder = new QRCodeEncoder(null,
bundle,
Contents.Type.CONTACT,
BarcodeFormat.QR_CODE.toString(),
smallerDimension);

bitmap = qrCodeEncoder.encodeAsBitmap();

Log.i(Constants.TAG_GBACARD, "QR Encoded");

Log.i(Constants.TAG_GBACARD, "Bitmap: " + bitmap);



} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}


This is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at newgbacard.gbacard.com.gbacard.activities.ContactDetailActivity$10.run(ContactDetailActivity.java:336)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.app.Activity.runOnUiThread(Activity.java:5575)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at newgbacard.gbacard.com.gbacard.activities.ContactDetailActivity.showQRCode(ContactDetailActivity.java:316)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at newgbacard.gbacard.com.gbacard.activities.ContactDetailActivity$5.onClick(ContactDetailActivity.java:168)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.view.View.performClick(View.java:5184)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.view.View$PerformClick.run(View.java:20910)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.os.Looper.loop(Looper.java:145)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5942)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
10-22 08:51:36.491 10948-10948/newgbacard.gbacard.com.gbacard W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)


Please Help. Thanks

Answer

Please replace this

final ImageView qrCodeImageView = (ImageView) findViewById(R.id.qrCode);

with this

final ImageView qrCodeImageView = (ImageView) dialogLayout.findViewById(R.id.qrCode);
Comments