android - 我的应用在API 21以下的设备上崩溃

标签 android error-handling inflate-exception

我在一个Android应用程序上工作,当我在使用api 21的设备/模拟器上对其进行测试并降低时崩溃了,并在日志中给了我一条消息:

09-29 07:32:30.349 6212-6212/org.jazmawy.mahmood.vocabulary 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.jazmawy.mahmood.vocabulary, PID: 6212
java.lang.RuntimeException: Unable to start activity 

 ComponentInfo{org.jazmawy.mahmood.vocabulary/org.jazmawy.mahmood.vocabulary.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class ImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class ImageView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at 
org.jazmawy.mahmood.vocabulary.MainActivity.onCreate(MainActivity.java:52)

如果删除XML元素,应用程序将启动并提示MainActivity
而且似乎屏幕尺寸不会影响我的观察结果,这归因于API,但我可能错了

这是XML代码
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/green"
    android:contentDescription="@string/app_name" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="16dp"
    android:orientation="vertical"
    android:weightSum="4"
    app:layout_constraintBottom_toTopOf="@+id/mainActivityAdView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/button11"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/vocabulary_button"
        android:onClick="openUnitActivity" />

    <Button
        android:id="@+id/button13"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/collection_button"
        android:onClick="collectionOnClick" />

    <Button
        android:id="@+id/button9"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/contact_us_button"
        android:onClick="contactsUsOnClick" />

    <Button
        android:id="@+id/button14"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/aboutus_button"
        android:onClick="aboutUsOnClick" />

</LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/mainActivityAdView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    ads:adSize="BANNER"
    ads:adUnitId="AD_UNIT_ID"
    app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

我尝试清除一些不必要的XML属性,但无济于事
接下来我应该尝试什么?

最佳答案

非常简单,将您的图像粘贴到drawable中,然后将路径更改为app \ src \ main \ res \ drawable

关于android - 我的应用在API 21以下的设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566320/

相关文章:

c# - 是否有可能捕获您无法处理的异常(在 C# 中)?

error-handling - STM32 UART错误未清除标志

android - 使用 TabLayout 在 ViewPager 中的 Google map fragment 中膨胀异常

java - 保存某些内容并转到下一个屏幕时,应用程序崩溃并且不再打开

android - 像 whatsapp、viber、facebook 聊天这样的气泡

java - fragment 布局组件为空

android - 如何在 onClick 方法中获取按钮名称

android - 在选项卡布局中添加 gridview

javascript - Axios : which HTTP response status codes result in then() and which in catch()?

android - fragment 对话框中的 fragment 膨胀引发错误 "Fragment did not create a view"