简体   繁体   中英

how do i make asynctask throw WriterException?

When i try to write the code like this error occurs:

doInBackground(String...)' in 'com.....QrGenerator' clashes with 'doInBackground(Params...)' in 'android.os.AsyncTask'; overridden method does not throw 'com.google.zxing.WriterException'

Here is the code:

private class QrGenerator extends AsyncTask<String,Integer,Bitmap>{
    @Override
    protected Bitmap doInBackground(String... strings) throws WriterException  {

    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < strings.length; i++) {
       strBuilder.append(strings[i]);
    }
    String value = strBuilder.toString();

    BitMatrix bitMatrix;
    try {
        bitMatrix = new MultiFormatWriter().encode(
                value,
                BarcodeFormat.DATA_MATRIX.QR_CODE,
                QRcodeWidth, QRcodeWidth, null
        );

    } catch (IllegalArgumentException Illegalargumentexception) {

        return null;
    }
    int bitMatrixWidth = bitMatrix.getWidth();

    int bitMatrixHeight = bitMatrix.getHeight();

    int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

    for (int y = 0; y < bitMatrixHeight; y++) {
        int offset = y * bitMatrixWidth;

        for (int x = 0; x < bitMatrixWidth; x++) {

            pixels[offset + x] = bitMatrix.get(x, y) ?
                    getResources().getColor(R.color.QRCodeBlackColor) : getResources().getColor(R.color.QRCodeWhiteColor);
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

    bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
    return bitmap;
}

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        imageView.setImageBitmap(bitmap);
    }
}

You cannot throw additional exceptions in overriden methods, like doInBackground(...) . See this link . You have to catch the exception within your method. I modified your method to also catch the WriterException.

try {
    bitMatrix = new MultiFormatWriter().encode(
            value,
            BarcodeFormat.DATA_MATRIX.QR_CODE,
            33, 33, null
    );

} catch (IllegalArgumentException | WriterException e1) {
    e1.printStackTrace();
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM