我如何让 asynctask 抛出 WriterException?
how do i make asynctask throw WriterException?
当我尝试编写这样的代码时出现此错误:
doInBackground(String...)' 在 'com.....QrGenerator' 中与 'doInBackground(Params...)' 在 'android.os.AsyncTask' 中冲突;重写的方法不会抛出 'com.google.zxing.WriterException'
代码如下:
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);
}
}
您不能在覆盖的方法中抛出额外的异常,例如 doInBackground(...)
。参见 this link。您必须在方法中捕获异常。我修改了您的方法以捕获 WriterException。
try {
bitMatrix = new MultiFormatWriter().encode(
value,
BarcodeFormat.DATA_MATRIX.QR_CODE,
33, 33, null
);
} catch (IllegalArgumentException | WriterException e1) {
e1.printStackTrace();
return null;
}
当我尝试编写这样的代码时出现此错误:
doInBackground(String...)' 在 'com.....QrGenerator' 中与 'doInBackground(Params...)' 在 'android.os.AsyncTask' 中冲突;重写的方法不会抛出 'com.google.zxing.WriterException'
代码如下:
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);
}
}
您不能在覆盖的方法中抛出额外的异常,例如 doInBackground(...)
。参见 this link。您必须在方法中捕获异常。我修改了您的方法以捕获 WriterException。
try {
bitMatrix = new MultiFormatWriter().encode(
value,
BarcodeFormat.DATA_MATRIX.QR_CODE,
33, 33, null
);
} catch (IllegalArgumentException | WriterException e1) {
e1.printStackTrace();
return null;
}