gpt4 book ai didi

android - java.lang.IllegalArgumentException : width and height must be > 0 异常

转载 作者:行者123 更新时间:2023-11-30 00:55:50 26 4
gpt4 key购买 nike

我在 Playstore 上有一个应用程序在极少数设备上生成以下错误。

java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:775)
at com.my.app.fragments.ImageFragment$MyAsyncTask$1.void run()(SourceFile:209)
at android.os.Handler.handleCallback(Handler.java:745)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

错误发生在MyAsyncTask中的onPostExecute这一行

c.drawBitmap(Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.RGB_565), 0, 0, p);

这是我的代码

在我的 ImageFragment 中,在 onCreateView 中,我这样做

    ImageView img = (ImageView) rootView.findViewById(R.id.img);
final LinearLayout view= (LinearLayout) rootView.findViewById(R.id.view);
MyAsyncTask asyncTask= new MyAsyncTask(img, view);
asyncTask.execute(number);

然后在AsyncTask

class MyAsyncTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private LinearLayout linearLayout;

public MyAsyncTask(ImageView imageView, LinearLayout linearLayout) {
imageViewReference = new WeakReference<>(imageView);
this.linearLayout = linearLayout;
}

// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {

// Code skipped
return bitmap;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
blurred = CommonUtils.fastblur(bitmap, 0.2f, 20);
final Bitmap resultBitmap = Bitmap.createBitmap(blurred.getWidth(), blurred.getHeight(), Bitmap.Config.ARGB_8888);

final Canvas c = new Canvas(resultBitmap);
c.drawBitmap(blurred, 0, 0, null);

final Paint p = new Paint();
p.setAlpha(127);

linearLayout.post(new Runnable() {
@Override
public void run() {
c.drawBitmap(Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.RGB_565), 0, 0, p);
// Rest code skipped
}
});
}
}
}
}

在阅读了许多关于这个问题的 stackoverflow 帖子后,我发现如果 View 尚未准备好就会发生这种情况,因此它应该与 View 的 post 方法一起使用,并带有您自己的可运行对象。但我不明白为什么它仍然在某些设备上导致此错误,即 Samsung。有人知道如何解决这个问题吗?任何建议将不胜感激。谢谢!!

最佳答案

您的任务在此设备上运行速度太快,LinearLayout 仍未布局。您可以使用 ViewTreeObserver:

ViewTreeObserver vto = linearLayout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = linearLayout.getMeasuredWidth();
int height = linearLayout.getMeasuredHeight();
c.drawBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565), 0, 0, p);
// Rest code skipped
}
});

关于android - java.lang.IllegalArgumentException : width and height must be &gt; 0 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com