位图不在对话框中显示

时间:2016-10-22 07:57:47

标签: java android bitmap imageview barcode

我在“警报”对话框中设置位图时遇到问题。我在另一个类中创建了条形码,我想在对话框中显示条形码。它一直显示错误。从日志中,条形码正在成功创建。问题一直指向qrCodeImageView.setImageBitmap(bitmap);行。请帮忙。

这是活动中的方法。

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

这是创建条形码的方法

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

这是错误:

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)

请帮助。感谢

1 个答案:

答案 0 :(得分:2)

请替换此

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

用这个

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