Android java.lang.IllegalArgumentException:宽度和高度必须为&;燃气轮机;0
我在playstore上有一个应用程序,它在很少的设备上生成以下错误Android java.lang.IllegalArgumentException:宽度和高度必须为&;燃气轮机;0,android,android-layout,bitmap,Android,Android Layout,Bitmap,我在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.graphi
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
c.drawBitmap(Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.RGB_565), 0, 0, p);
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
}
});
}
}
}
}
这是我的密码
在我的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
c.drawBitmap(Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.RGB_565), 0, 0, p);
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
}
});
}
}
}
}
类MyAsyncTask扩展了AsyncTask{
私有最终WeakReference imageViewReference;
私人线性布局线性布局;
公共MyAsyncTask(ImageView ImageView、LinearLayout LinearLayout){
imageViewReference=新的WeakReference(imageView);
this.linearLayout=linearLayout;
}
//在背景中解码图像。
@凌驾
受保护位图doInBackground(整数…参数){
//跳过的代码
返回位图;
}
@凌驾
受保护的void onPostExecute(位图){
if(位图!=null){
最终ImageView=imageViewReference.get();
如果(imageView!=null){
设置图像位图(位图);
模糊=普通。快速模糊(位图,0.2f,20);
最终位图结果位图=Bitmap.createBitmap(fuzzle.getWidth()、fuzzle.getHeight()、Bitmap.Config.ARGB_8888);
最终画布c=新画布(resultBitmap);
c、 drawBitmap(模糊、0、0、空);
最终油漆p=新油漆();
p、 setAlpha(127);
linearLayout.post(新的Runnable(){
@凌驾
公开募捐{
c、 drawBitmap(Bitmap.createBitmap(linearLayout.getWidth(),linearLayout.getHeight(),Bitmap.Config.RGB_565),0,0,p);
//已跳过Rest代码
}
});
}
}
}
}
在阅读了许多关于这个问题的stackoverflow文章之后,我发现如果视图还没有准备好,就会发生这种情况,因此应该将它与视图的post方法一起使用,并使用您自己的runnable。但是我不明白为什么它仍然会在一些设备上导致这个错误,比如三星的
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
}
});
谢谢你的回答。我一定会试试的。顺便说一句,为什么ViewTreeObserver会工作?因为您试图在计算布局之前获得布局大小(即,您仍然不知道尺寸,因为Android尚未计算尺寸)。ViewTreeObserver允许您在发生这种情况时接收回调:“视图树观察者用于注册监听器,这些监听器可以收到视图树中全局更改的通知。此类全局事件包括但不限于整个树的布局、绘图过程的开始、触摸模式更改……”顺便说一句,我已经编辑了答案,修正了一些变量名错误。Thnx的答复,将尝试它,并接受它,如果这项工作非常感谢!!