View with svg background crashes on android 6.0 and lower - android

I have the following situation: I have a constraint layout with background:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_wave_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_wave_header_svg_drawable"
android:paddingBottom="41dp"
app:layout_constraintTop_toTopOf="parent">
Here's the ic_wave_header_svg_drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_wave_header" />
</layer-list>
Here's the ic_wave_header:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:tools="http://schemas.android.com/tools"
android:width="328dp"
android:height="199dp"
android:viewportWidth="328"
android:viewportHeight="199"
tools:ignore="VectorRaster">
<path
android:fillType="evenOdd"
android:pathData="M328,198.147C328,198.147 287.806,164.737 164,168.868C40.194,172.999 0,140 0,140V198.147V0H328L328,198.147Z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="351.346"
android:endY="198.147"
android:startX="351.346"
android:startY="-28.2069"
android:type="linear">
<item
android:color="#FF9DDE10"
android:offset="0" />
<item
android:color="#FF82BD00"
android:offset="1" />
</gradient>
</aapt:attr>
</path>
</vector>
When I run the app, it crashes on android 6.0 and lower. In the other versions, it works correctly. What's the problem and how can I solve it?
UPD
Error log:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_wave_header.xml from drawable resource ID #0x7f080180
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.graphics.drawable.LayerDrawable.updateLayerFromTypedArray(LayerDrawable.java:296)
at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:239)
at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:164)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
at android.content.res
It seems that it is something incorrect with drawable, but if it is right, why it doesn't crash on other devices

In your application class -> enable setCompatVectorFromResourcesEnabled
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);//add this
}

This crash is caused by using
android:offset="0" in svg file of your drawable.
To fix this you can either avoid using android:offset="0" in svg file (by using plain color instead of gradient) or you can use .png format of your drawable image for quick fix.

Related

Resource not found exception for ShapeableImageView using vector?

I am using ShapeableImageView in my XML as
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_unread_msg"
/>
I am getting Resource not found exception when I run the app on OS 5 and 6
Caused by android.content.res.Resources$NotFoundException: File res/drawable/ic_unread_msg.xml from drawable resource ID #0x7f0700f7
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2850)
at android.content.res.Resources.loadDrawable(Resources.java:2732)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:74)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:96)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:92)
at java.lang.reflect.Constructor.newInstance(Constructor.java)
Caused by android.content.res.Resources$NotFoundException: File res/drawable/$ic_unread_msg__0.xml from color state list resource ID #0x7f07000b
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2960)
at android.content.res.Resources.loadColorStateList(Resources.java:2909)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
at android.graphics.drawable.VectorDrawable$VFullPath.updateStateFromTypedArray(VectorDrawable.java:1605)
at android.graphics.drawable.VectorDrawable$VFullPath.inflate(VectorDrawable.java:1584)
at android.graphics.drawable.VectorDrawable.inflateInternal(VectorDrawable.java:666)
at android.graphics.drawable.VectorDrawable.inflate(VectorDrawable.java:571)
Caused by org.xmlpull.v1.XmlPullParserException: Binary XML file line #9: invalid color state list tag gradient
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2956)
at android.content.res.Resources.loadColorStateList(Resources.java:2909)
at android.content.res.TypedArray.getColor(TypedArray.java:439)
This is my vector image
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="14dp"
android:height="14dp"
android:viewportWidth="14"
android:viewportHeight="14">
<path android:pathData="M7,0L7,0A7,7 0,0 1,14 7L14,7A7,7 0,0 1,7 14L7,14A7,7 0,0 1,0 7L0,7A7,7 0,0 1,7 0z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="7"
android:endY="14"
android:startX="7"
android:startY="0"
android:type="linear">
<item
android:color="#FF63D8FF"
android:offset="0" />
<item
android:color="#FF6397FF"
android:offset="1" />
</gradient>
</aapt:attr>
</path>
</vector>
Can't figure out why the crash is happening?
Thanx to Sandi's comment
Replacing android:src with app:srcCompat works

Android Studio - Fraction values must be in the range 0 to 1: 3.0

<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:tools="http://schemas.android.com/tools"
android:width="1080dp"
android:height="592dp"
android:viewportWidth="1080"
android:viewportHeight="592"
tools:ignore="VectorRaster">
<path
android:pathData="M0,440.31L0,0L1080,0L1080,440.31C952.58,532.86 758.02,592 540,592S127.42,532.86 0,440.31Z">
<aapt:attr name="android:fillColor">
<gradient
android:startY="14.208"
android:startX="-280.8"
android:endY="380.656"
android:endX="921.24"
android:type="linear">
<item android:offset="0" android:color="#602EC4"/>
<item android:offset="1" android:color="#9C7CDD"/>
<item android:offset="3" android:color="#8355D1"/>
</gradient>
</aapt:attr>
</path>
I am using this code to create a drawable for my app. but I keep getting this error: Fraction values must be in the range 0 to 1: 3.0. My Android Studio Version is 3.6.2

Android vector asset fatal exception after pull from version control

We use Bitbucket and after the last commit, vector images don't work.
12-03 12:48:58.214 26158-26158/linegraphapp.linegraphapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: linegraphapp.linegraphapp, PID: 26158
android.content.res.Resources$NotFoundException: Resource ID #0x7f060064
at android.content.res.Resources.getValue(Resources.java:1378)
If I create the same button in my local copy, all works fine.
what could be wrong?
My layout:
<?xml version="1.0" encoding="utf-8"?>
<menu
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"
tools:context=".AddPicture">
<item
android:id="#+id/plus_btn"
android:icon="#drawable/plus_btn"
android:orderInCategory="2"
android:title="Add"
app:showAsAction="always"></item>
</menu>
Button code:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="34dp"
android:height="34dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFF"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM17,13h-4v4h-2v-4L7,13v-2h4L11,7h2v4h4v2z"/>
If it needs, I will add full stack trace.
project structure
I use compileSdkVersion 26 and Android Studio 3.2.1

Why do we use drawable v24 in android?

We have a drawable file already in drawable v21 also for ripple effect.
So why we use drawable v24 when we are getting vector file already.
Below is the xml File ic_launcher_foreground.xml.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0"/>
<item
android:color="#00000000"
android:offset="1.0"/>
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11
40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81
70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76
60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1"/>
</vector>
The different drawable folders are for providing different screen densities for device compatibility and for different Android versions.
See this answer for more details

Error : <item> tag requires a 'drawable' attribute or child tag defining a drawable

I created a state-list selector file item_bg_selector.xml in the drawable folder like this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/item_background_selected" />
<item android:state_focused="true"
android:drawable="#drawable/item_background_selected" />
<item android:state_pressed="true"
android:drawable="#drawable/item_background_selected" />
<item android:drawable="#android:color/transparent"/>
</selector>
Then i define a style element
<style name="Item">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/item_bg_selector</item>
</style>
Then I try to create an ImageButton like this :
<ImageButton style="#style/Item"
android:id="#id/btn_home"
android:src="#drawable/home_button" />
Eclipse keeps giving me the following error in the Event Details window :
Failed to parse file /path/to/projectdir/res/drawable/item_bg_selector.xml
The exception trace is given below :
org.xmlpull.v1.XmlPullParserException: Binary XML file line #16: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208)
at com.android.layoutlib.bridge.android.BridgeTypedArray.getDrawable(BridgeTypedArray.java:731)
at android.view.View.<init>(View.java:1885)
at android.widget.ImageView.<init>(ImageView.java:112)
at android.widget.ImageButton.<init>(ImageButton.java:85)
at android.widget.ImageButton.<init>(ImageButton.java:81)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
at com.android.layoutlib.bridge.android.BridgeInflater.onCreateView(BridgeInflater.java:84)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:129)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:296)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:279)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:318)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1317)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1071)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:897)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(LayoutEditor.java:384)
at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:290)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195)
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2743)
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429)
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3629)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3284)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
Now, The question is where is the error? the drawables exist in the folder. tag has a drawable attribute. I have cross checked this close to 15 times and still I have not been able to find the problem. I believe it is a very simple problem(if any). Any work around to get over with this is also appreciated.
Addition :
Also, line 16 in item_bg_selector.xml corresponds to the third line from the top in the file I have posted. that is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
line16 ----> android:drawable="#drawable/item_background_selected" />
The error has to do with the format of your xml, you are setting something like
android:background="#drawable/item_bg_selector"
What the error is telling you is that it should be like this
android:drawable="#drawable/item_bg_selector"
I've experienced the same Exception for a different problem.
I was trying to use a color selector for the View's android:background:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tab_background_selector" />
<!-- /res/color/tab_background_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_checked="true" />
<item android:color="#color/light_blue" />
</selector>
Moving the selector to the drawable folder and changing android:color to android:drawable solved the problem.
Also, the background definition in the Layout needs to be changed to android:background="#drawable/tab_background_selector"
Here is the final selector:
<!-- /res/drawable/tab_background_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_checked="true" />
<item android:drawable="#color/light_blue" />
</selector>
I had the same error and switching the order of the attributes (as it was suggested in the this answer to this other question) to have the drawable the first one solved the problem.
In your case would be switching:
<item android:state_selected="true"
android:drawable="#drawable/item_background_selected" />
to:
<item android:drawable="#drawable/item_background_selected"
android:state_selected="true" />
Finally, I got to know that its a problem with just ADT and it actually runs fine on the device. This corresponds to issue 21046.
similar to this answer, I need to make a drawable version of the color resource.
solution
instead of modifying the existing color state list file, craete a new drawable file
new_drawable_file.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/old_color_state_list" />
</shape>
then i updated the problematic usages of #color/old_color_state_list to #drawable/new_drawable_file.
background
in my styles.xml, i had overridden ?android:activatedBackgroundIndicator with my #color/old_color_state_list
<item name="android:activatedBackgroundIndicator">#color/old_color_state_list</item>
but the documentation for ?android:activatedBackgroundIndicator says it should be a drawable resource, not a color:
Drawable used as a background for activated items.
so, i updated my styles.xml to override ?android:activatedBackgroundIndicator with my #drawable/new_drawable_file drawable, instead.
<item name="android:activatedBackgroundIndicator">#drawable/new_drawable_file</item>
I also encountered the same issue.
I just restarted eclipse with -clean, and the did a project -> Clean on the project in question and the errors have gone away.
Restarting Eclipse and cleaning the project worked for me.
The same error occurs on the activity_main.xml file if there is an extra LinearLayout in the Activity. Removing this solves the issue.
Cleaning and other things do not help.
The drawable inside the file (a png) was corrupted. I just saved again the file and it worked.
I got it to work by copying the image right into the file system (Finder on Mac) rather than through Android Studio.
I had the problem because when renaming the selector for pre-lollipop devices, I forgot to rename the ripple with th e same name also in the drawable-v21 folder.
In my case I was applying color attribute to checkedIcon attribute, Instead
of chipIconTint, so replacing checkedIcon with chipIconTint in style solved
my problem.
Use
<item name="chipIconTint">#color/chip_text_color</item>
Instead of
<item name="checkedIcon">#color/chip_text_color</item>

Categories

Resources