M
M
MCjores2019-06-03 23:33:23
Android
MCjores, 2019-06-03 23:33:23

How to use vector in toolbar?

There is an ImageView, I use a vector as an image, but the application gives an error

Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #28: Error inflating class android.support.v7.widget.AppCompatImageView
Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.v7.widget.AppCompatImageView

markup
<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorWhite"
           
            <android.support.v7.widget.AppCompatImageView

                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="100dp"
                android:layout_marginEnd="100dp"
                app:srcCompat="@drawable/ic_logo"
                />

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.mccorporation.mcjores.askmollyproject"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation "com.android.support:support-vector-drawable:28.0.0"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Just in case added to MainActivity
static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

Tried to programmatically put - the same thing.
ImageView image = findViewById(R.id.image);
        Drawable drawable = VectorDrawableCompat.create(getResources(),R.drawable.ic_logo, null);
        image.setImageDrawable(drawable);

log cat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mccorporation.mcjores.askmollyproject/com.mccorporation.mcjores.askmollyproject.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #28: Error inflating class android.support.v7.widget.AppCompatImageView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6698)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
     Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #28: Error inflating class android.support.v7.widget.AppCompatImageView
     Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.v7.widget.AppCompatImageView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:651)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:999)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:
     Caused by: android.content.res.Resources$NotFoundException: Drawable com.mccorporation.mcjores.askmollyproject:drawable/ic_logo with resource ID #0x7f070073
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_logo.xml from drawable resource ID #0x7f070073
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:804)
2019-06-03 23:07:37.655 29177-29177/? E/AndroidRuntime:     at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:617)
        at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:316)
        at android.content.res.Resources.getDrawableForDensity(Resources.java:890)
        at android.content.res.Resources.getDrawable(Resources.java:832)
        at android.content.Context.getDrawable(Context.java:614)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mitaichik, 2019-06-04
@MCjores

There was a similar problem, with icons from material.io
The problem was in the SVG file itself (or in the studio that incorrectly converted it) - the path instead of 1.9 had the number 1.1.9 - because of this, a NotFoundException popped up. The latest version of the studio shows such places in red - it says that there is an error, the old versions do not.
But the problem did not appear on all devices - on most it passed, but on some - it crashed like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question