生成签名apk时,android 3.1.2中的imageview导致错误

时间:2018-04-29 17:49:32

标签: android

我可以在手机上安装debug apk或build apk 下面是xml,我只使用drawable而不是像drawable-21或24那样请所以请查看我的xml文件我只使用png图像................ ..................................................

<?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"
    android:background="@drawable/gradient_background">

    <ImageView
        android:id="@+id/a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="18dp"
        android:src="@drawable/u_text_logo"
        app:layout_constraintBottom_toTopOf="@+id/imageView1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/your_friend"
        app:layout_constraintBottom_toTopOf="@+id/imageView3"
        app:layout_constraintEnd_toEndOf="parent" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="182dp"
        android:layout_height="223dp"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/web_hi_res"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.578" />

    <ProgressBar
        android:id="@+id/progressBar3"
        android:layout_width="80dp"
        android:layout_height="55dp"

        android:layout_marginBottom="108dp"
        android:layout_marginTop="8dp"
        android:padding="1dp"
        android:theme="@style/AppTheme.WhiteAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />


</android.support.constraint.ConstraintLayout>

但是当我生成签名的apk时,我得到像这样的错误崩溃

java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.xx/xxx.xxx.xx.SplasScreen}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class ImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515)
    at android.app.ActivityThread.access$1000(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5571)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class ImageView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.ag.b(Unknown Source)
    at android.support.v7.app.r.setContentView(Unknown Source)
    at hemanth.com.vtuquestionpaper2017.SplasScreen.onCreate(Unknown Source)
    at android.app.Activity.performCreate(Activity.java:6357)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
    ... 9 more
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class ImageView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:786)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
    ... 17 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.at.d()' on a null object reference
    at android.support.v7.widget.AppCompatImageView.setImageDrawable(Unknown Source)
    at android.widget.ImageView.<init>(ImageView.java:154)
    at android.widget.ImageView.<init>(ImageView.java:140)
    at android.support.v7.widget.AppCompatImageView.<init>(Unknown Source)
    at android.support.v7.widget.AppCompatImageView.<init>(Unknown Source)
    at android.support.v7.app.av.a(Unknown Source)
    at android.support.v7.app.ag.a(Unknown Source)
    at android.support.v7.app.ag.onCreateView(Unknown Source)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
    ... 21 more

2 个答案:

答案 0 :(得分:1)

此问题仅在发布版本中创建,因为proguard会从代码中删除一些有用的类或方法。

您可以从Google dev docs阅读并理解proguard的规则。或Basics

现在您可以将此规则添加到proguard-rules.pro文件

-keep class android.support.v7.widget.** { *; }

添加此规则后尝试清理构建并制作发布apk。

这是proguard-rules.pro位于

proguard

我也告诉你捷径;)

删除proguard设置,以便android不会删除任何缩小代码的类。在您的应用级build.gradle更改

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

到这个

buildTypes {
    release {
    }
}

然后同步并构建apk。它应该运行。

更新

Proguard是免费的Java类文件收缩器,优化器,混淆器和预验证器。它检测并删除未使用的类,字段,方法和属性。它优化字节码并删除未使用的指令。它使用简短的无意义名称重命名剩余的类,字段和方法。

答案 1 :(得分:0)

尝试在活动的onCreate()方法中添加以下行:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

将以下属性添加到xml中的 ImageView:

app:srcCompact 

而不是android:src