我的应用在API 21及更低版本的设备上崩溃

时间:2018-09-29 07:48:31

标签: 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属性,但无济于事 接下来我应该尝试什么?

1 个答案:

答案 0 :(得分:0)

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