java.lang.IllegalArgumentException:width和height必须是> 0

时间:2016-10-21 12:54:05

标签: 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.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)

onPostExecute

中的MyAsyncTask中的此行发生错误
c.drawBitmap(Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.RGB_565), 0, 0, p);

这是我的代码

在我ImageFragment的{​​{1}}我这样做

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

在阅读了很多关于这个问题的stackoverflow帖子后,我发现如果视图还没有准备就会发生这种情况,因此它应该与View的post方法一起使用你自己的runnable。但我无法理解为什么它仍然会在某些设备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 } }); } } } } 上造成此错误。有人知道如何解决这个问题吗?任何建议将不胜感激。谢谢!!

1 个答案:

答案 0 :(得分:1)

您的任务在此设备上运行得太快,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
    }
});
相关问题