当我想在图像按钮上添加图像时,出现此异常:
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)在 drawable 文件夹中,所以我不明白为什么会出现这个异常。我也曾尝试重建该项目。任何帮助将被appriciated。
这可能是因为您将 drawable 放在错误的文件夹中。例如,如果您的目标是 API 20(或更低版本),请确保将 png 文件放在 drawable 而不是 drawable-v21 中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句