我的应用在我的手机上完美运行。在一些旧设备上,它崩溃了:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blub.bla/com.blub.bla.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
...
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class ImageButton
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at
...
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_google_plus_icon.xml from drawable resource ID #0x7f0700ab
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2872)
...
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #0: invalid color state list tag gradient
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
at
...
我检查了其他帖子,大部分时间都与gradle有关,我可以说我已设置vectorDrawables.useSupportLibrary = true
。
Gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
repositories {
mavenCentral()
}
defaultConfig {
applicationId "com.blub.bla"
vectorDrawables.useSupportLibrary = true
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
android {
lintOptions {
disable 'RestrictedApi'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.facebook.android:facebook-login:[4,5)'
implementation 'com.facebook.android:facebook-share:[4,5)'
implementation 'com.android.support:appcompat-v7:27.0.1'
implementation 'com.android.support:design:27.0.1'
implementation 'com.android.support:support-v4:27.0.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
implementation 'com.google.android.gms:play-services-location:10.+'
implementation 'com.google.android.gms:play-services-places:10.+'
implementation 'com.google.maps.android:android-maps-utils:0.4+'
implementation 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
testImplementation 'junit:junit:4.12'
}
修改: activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/bg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/activity_main_drawer">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<com.facebook.share.widget.ShareButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="@+id/fb_share_button"/>
<ImageButton
android:id="@+id/sm_twitter"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_twitter_bird_logo"
android:background="@color/transparent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="@+id/sm_facebook"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_f_icon_color"
android:background="@color/transparent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/sm_twitter"
/>
<ImageButton
android:id="@+id/sm_gplus"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_google_plus_icon"
android:background="@color/transparent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/sm_facebook"
/>
</RelativeLayout >
</RelativeLayout>
</android.support.design.widget.NavigationView>>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@drawable/bg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_filter"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="175dp"
android:orientation="horizontal">
<include
layout="@layout/filter_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
有谁知道如何解决这个问题? :(
答案 0 :(得分:1)
在24 API之前,您无法在矢量绘图中使用渐变。
关于android:fillColor
,例如,来自https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html:
指定用于填充路径的颜色。可以是颜色,或者对于SDK 24+,颜色状态列表或渐变颜色
答案 1 :(得分:0)
第一个问题:我在drawable/v21
而不是drawable
中有一些资源(不容易看到,因为它在Android工作室中显示相同的文件夹,只需在文件名旁边加(21)
第二个问题:使用app:srcCompat
代替android:background
或android:res
答案 2 :(得分:0)
当您尝试在21岁以下的Android版本中显示SVG时,您可以使用AppCompatImageButton而不是ImageButton或AppCompatImageView与app:srcCompat