java - 图像按钮的Android问题
问题描述
当我想在图像按钮上添加图像时,我得到了这个异常:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.admin.
tetrisapp/com.example.admin.tetrisapp.MainActivity}:
android.view.InflateException: Binary XML file line #28: Error inflating
class ImageButton
Caused by: android.content.res.Resources$NotFoundException: Resource is
not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060056 a=-1
r=0x7f060056}
这是我的xml代码:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3">
<ImageButton
android:id="@+id/rightButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/downButton"
android:layout_alignParentEnd="true"
android:layout_marginEnd="40dp"
android:src="@drawable/square"/>
</RelativeLayout>
</LinearLayout>
我的 MainActivity :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我的图片(png)在可绘制文件夹中,所以我不明白为什么会出现此异常。我也尝试过重建项目。任何帮助都将不胜感激。
解决方案
这可能是因为您将可绘制对象放在错误的文件夹中。例如,如果您的目标是 API 20(或更低版本),请确保将您的 png 文件放在 drawable 而不是 drawable-v21 中。
推荐阅读
- c# - 如何在 .Net Blazor 中获取 Javascript 回调?
- python - 如何访问在数据框中保存为对象的分类数据?
- python - Python Paramiko SFTP 打开文件从 N 行开始
- typescript - 使用 typescript 将组件传递给 apolloprovider
- oracle - (Oracle)如何在不创建另一个表的情况下方便地测试用户功能
- python - 如何在 Windows 中为 VS Code 手动安装 python 包?
- mysql - laravel中的列选择不区分大小写
- java - 无法连接到 intellij 中的 Wildfly 服务器
- php - 如何将一个键与数组组合为值?
- css - React 导入的 CSS 不可用