Android本地笔记应用开发遇Bitmap创建报错:width and height must be >0
问题:Bitmap.createBitmap抛出width/height必须>0异常
我是Android开发新手,正在开发一款基于本地数据库的笔记应用。所有布局视图的宽高均设置为大于0的值,但仍抛出java.lang.IllegalArgumentException: width and height must be >0异常,报错堆栈指向android.graphics.Bitmap.createBitmap方法。
相关布局代码
- RecyclerView列表项布局(TextView)
<TextView android:id="@+id/note_item" android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="None" />
- 首页Fragment布局(包含RecyclerView)
<soup.neumorphism.NeumorphCardView android:id="@+id/card_edit" android:layout_width="match_parent" android:layout_height="190dp" android:background="@color/background_color" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <EditText android:id="@+id/note" android:inputType="text" android:layout_width="488dp" android:layout_height="48dp" android:autofillHints="" /> </soup.neumorphism.NeumorphCardView> <soup.neumorphism.NeumorphButton android:id="@+id/addButton" style="@style/Widget.Neumorph.Button" android:layout_width="150dp" android:layout_height="60dp" android:text="Add Note" app:layout_constraintHorizontal_bias="0.532" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/card_edit" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="400dp" android:orientation="vertical" app:layout_constraintTop_toBottomOf="@id/addButton" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="259dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> <soup.neumorphism.NeumorphButton android:id="@+id/clearButton" style="@style/Widget.Neumorph.Button" android:layout_width="150dp" android:layout_height="40dp" android:layout_marginTop="50dp" android:text="Clear Note" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.564" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/recycler_view" app:layout_constraintVertical_bias="1.0" />
报错日志
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.notelocal, PID: 26959 java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:877) at android.graphics.Bitmap.createBitmap(Bitmap.java:856) at android.graphics.Bitmap.createBitmap(Bitmap.java:823)
排查与解决方案
- 检查第三方Neumorph控件的绘制逻辑
你使用的soup.neumorphism系列控件,内部会通过Bitmap绘制拟物阴影效果,如果控件在布局测量完成前就触发绘制,会获取到0值宽高,导致Bitmap创建失败。
- 给Neumorph控件添加明确最小宽高:
app:neumorph_minWidth="100dp" app:neumorph_minHeight="50dp" - 延迟初始化控件:在Fragment的
onViewCreated中通过view.post(Runnable)触发控件相关逻辑,确保布局已完成测量。
- 检查自定义Bitmap创建代码
如果业务代码中有手动调用Bitmap.createBitmap的逻辑(如截图、生成图片),需确保传入的宽高参数有效:
- 避免在
onCreate/onCreateView阶段直接获取视图宽高,此时视图未完成测量,会返回0,改用view.post()获取真实宽高。 - 添加参数校验:
if (width <= 0 || height <= 0) { // 跳过创建或处理异常,避免崩溃 return; } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- 检查RecyclerView适配器中的视图操作
如果适配器中对列表项有截图或Bitmap生成操作,需确保视图已完成测量,可在onBindViewHolder中通过holder.itemView.post()延迟执行相关逻辑。
内容的提问来源于stack exchange,提问作者Aditya
相关产品推荐
相关产品推荐

