Jetpack Compose: painterResource randomly crashes with ArrayIndexOutOfBoundsException with different vector icons - android

Did some searching, but didn't find anything about this on SO.
This function call crashes, but very rarely and seemingly randomly, with several different vector icon assets (all of which work fine almost all of the time):
androidx.compose.ui.res.painterResource(iconRes)
Full usage example within my composable code:
Icon(
painter = painterResource(R.drawable.ic_add_row),
tint = TextBlackSecondary,
contentDescription = ""
)
It's always ArrayIndexOutOfBoundsException, but I've seen somewhat different stack traces:
java.lang.ArrayIndexOutOfBoundsException
length=2; index=2
java.lang.FloatingDecimal.floatValue (FloatingDecimal.java:1695)
java.lang.Float.parseFloat (Float.java:459)
androidx.compose.ui.graphics.vector.PathParser.getFloats (PathParser.java:569)
androidx.compose.ui.graphics.vector.PathParser.parsePathString (PathParser.java:83)
androidx.compose.ui.graphics.vector.VectorKt.addPathNodes (Vector.kt:75)
androidx.compose.ui.graphics.vector.compat.XmlVectorParser_androidKt.parsePath (XmlVectorParser_androidKt.java:283)
androidx.compose.ui.graphics.vector.compat.XmlVectorParser_androidKt.parseCurrentVectorNode (XmlVectorParser_androidKt.java:101)
androidx.compose.ui.res.VectorResources_androidKt.loadVectorResourceInner (VectorResources_androidKt.java:81)
androidx.compose.ui.res.PainterResources_androidKt.loadVectorResource (PainterResources_androidKt.java:95)
androidx.compose.ui.res.PainterResources_androidKt.painterResource (PainterResources_androidKt.java:65)
Another:
java.lang.ArrayIndexOutOfBoundsException
src.length=1 srcPos=0 dst.length=26 dstPos=0 length=2
java.lang.System.arraycopy (System.java:458)
java.lang.FloatingDecimal.getChars (FloatingDecimal.java:930)
java.lang.FloatingDecimal.toJavaFormatString (FloatingDecimal.java:915)
java.lang.Float.toString (Float.java:206)
android.util.TypedValue.coerceToString (TypedValue.java:469)
android.content.res.XmlBlock$Parser.getAttributeValue (XmlBlock.java:212)
android.content.res.XmlBlock$Parser.getAttributeValue (XmlBlock.java:232)
androidx.core.content.res.TypedArrayUtils.hasAttribute (TypedArrayUtils.java:58)
androidx.core.content.res.TypedArrayUtils.getNamedFloat (TypedArrayUtils.java:70)
androidx.compose.ui.graphics.vector.compat.AndroidVectorParser.getNamedFloat (XmlVectorParser.android.kt:591)
androidx.compose.ui.graphics.vector.compat.XmlVectorParser_androidKt.createVectorImageBuilder (XmlVectorParser_androidKt.java:160)
androidx.compose.ui.res.VectorResources_androidKt.loadVectorResourceInner (VectorResources_androidKt.java:89)
androidx.compose.ui.res.PainterResources_androidKt.loadVectorResource (PainterResources_androidKt.java:96)
androidx.compose.ui.res.PainterResources_androidKt.painterResource (PainterResources_androidKt.java:65)
Why do the same vector assets usually work and sometimes crash?
A bug somewhere in Jetpack Compose?
Using androidx.compose.ui:ui:1.3.2

Related

Adding a style to custom text view in compose

I have a CustomTextView, I used to add style to it using xml style attribute, but with compose I need to understand how to do it.
When using this :
AndroidView( factory = { context->
RegularAmountTextView(ContextThemeWrapper(context,R.style.TextAppearance_MyTheme_Heading_Small ),null ).apply {
text ="1234"
}
}, modifier = Modifier.fillMaxWidth().wrapContentHeight() )
I get error: Infinite cycle trying to resolve '?attr/textColorHighlight': Render may not be accurate. in preview panel.
while on running on device I get:
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
at android.content.res.TypedArray.getColor(TypedArray.java:529)
at android.widget.TextView.readTextAppearance(TextView.java:4345)
at android.widget.TextView.<init>(TextView.java:1340)
at android.widget.TextView.<init>(TextView.java:1258)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:108)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:103)
Similar error only. If I remove the style then it works. Has Android team provided any solution for this.
Another question: On facing these errors I was thinking is using compose on production a wise decision, or we should wait till android team provides proper inter-portability.

PainterResource throws IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported

I am working on a Jetpack Project which downloads an image from API using Coil Library.
I have confirmed that valid image URLs and related data are being returned from the API call successfully. I am able to view the image on my browser using the returned URL.
This is my Image Composable:
#Composable
fun AstroPhoto(picture: AstroPicture) {
val imgUrl = picture.url.toUri()
.buildUpon()
.scheme("https")
.build()
AsyncImage(model = ImageRequest.Builder(LocalContext.current)
.data(imgUrl)
.size(Size.ORIGINAL)
.crossfade(true).build(),
placeholder = painterResource(R.drawable.loading_animation),
contentDescription = picture.title,
contentScale = ContentScale.Crop,
)
}
I call the above image composable in a Lazy Column:
....
Box( ... ) {
LazyColumn(content = {
items(state.astroPictures) {
AstroPhoto(picture = it)
} })
}
This is the exception at I am getting:
FATAL EXCEPTION: main Process: com.uxstate, PID: 31790
java.lang.IllegalArgumentException: Only VectorDrawables and
rasterized asset types are supported ex. PNG, JPG
at androidx.compose.ui.res.PainterResources_androidKt.loadVectorResource(PainterResources.android.kt:93)
I am on compose_version = '1.1.1' working with kotlin_version = '1.6.10'. My coil version is "io.coil-kt:coil-compose:2.1.0" and compileSdk 32 and AS Chipmunk. Source code can be found here.
I have checked the docs and searched online but cannot resolve the error. Please help me to go about the error, Thanks.
if you are using png and jpg with painterResource but still face this issue. Change your image, It happened when there is something wrong with the image.
painterResource converts png, and jpg in the bitmap. Its throws that error if it fails to convert any Resource
My project worked correctly before, but after I changed some code (just add a fucntion, it is not about png or jpg or other things about image), my app crash when render a png. But after i reinstall the project in the emulator, it is fixed.
It seems like a bug in Compose? I dont know, may be a bug in gradle?
Just try to reinstall your app, i think it will be fixed.
in my previous projects, there is no problem passing drawable resources to painterResource function. But when I create a new jetpack-compose project, the problem you describe occurs.
In my new project, this problem only happens in preview phase. When I firstly build and run my app in emulator, the problem suddenly disappears.
so this is my solution: if you haven't run you app in emulator, build and run it in emulator.
Just encountered this error and see if anyone had the same error as me.
I just re-opened the Android Studio, and after that, I use Clean Project and Rebuild Project. Now the project and the compose preview are working fine.
Tried to use shape drawable as a placeholder and it caused this error.
If I set the same placeholder to ImageRequest.Builder instead, the code works fine.
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(imageUrl)
.placeholder(R.drawable.placeholder_rect)
.error(R.drawable.placeholder_rect)
.build(),
contentDescription = "",
)

Iconize Xamarin with Fontawesome

I'm new to Xamarin and I'm trying to use the Iconize NuGet package for Xamarin, but I'm not having much luck. At the moment I'm working with a simple Android app. I've installed Iconize per the instructions and, save for an error on the line:
FormsPlugin.Iconize.Droid.IconControls.Init(Resource.Id.toolbar, Resource.Id.tabs);
(the compiler didn't like the Resource.Id.toolbar or Resource.Id.tabs so I removed it) everything compiles and runs. However, when I try to add an IconButton with a Fontawesome image, I get "System.NullReferenceException: Object reference not set to an instance of an object." error that points back to
the line LoadApplication( new App() ); in the MainActivity.cs.
I'm trying to add the IconButton to a grid in code (not XAML) using
grid.Children.Add( new IconButton
{
Image = "fa-info-circle"
}, 3, 2 );
Any ideas on how to make this work? The examples on the Iconize page haven't been very useful and I haven't found any examples on Google.
Okay I finally found something that was useful. I found a clue on this page and some other information on the project issues page on Github.
Ultimately, to get the icon to display I used
grid.Children.Add( new IconButton
{
Text = "fa-info-circle",
}, 3, 2 );
It was the Text property and not the Image property that I should have used.
Hope this helpful to someone else.

Xamarin ContentPage BackgroundImage property crashes app on Android

I'm using Xamarin Forms to produce a TabbedPage consisting of more ContentPages. This is the part of code causing trouble:
public void launchMainDesign(object s, EventArgs e) {
MainPage = new TabbedPage {
Children = {
new ContentPage {
Title = "Login",
Content = pages.loginContent,
BackgroundImage = "bgmain.jpg"
},
new ContentPage {
Title = "Sign Up",
Content = pages.signUpContent,
BackgroundImage = "bgmain.jpg"
}
}
};
}
It seems absolutely fine. I have both the images in my Drawable directory, with the build action set to "AndroidResource".
Whenever the launchMainDesign() function is fired by pressing a button, the app crashes immediately, both in emulator and a build version of the app on a tablet. Unfortunately, I can't test on iOS and WP.
I even tried putting the whole inside part of the function in a try/catch block and print out the exception, but the app just crashes nevertheless.
I am desperately trying to solve this simple problem for about a week now. No one seems to be having exactly the same issue as me. Weirdest thing is, I have a different app where I use exactly the same method and it works just fine. Can the Android Theme be causing this (I'm using Holo, in the working app, there's no theme specified)? That seems to be the only difference.
I also don't think this is caused by RAM struggles, as the image is only about 700 kilobytes (1080x1920) - for this example, I've only used one image.
It could be a memory issue, because even do the size is not big depending on the device resolution it might be trying to scale the image to the device dimensions.
Try checking this README:
https://github.com/xamarin/customer-success/blob/master/samples/Xamarin.Forms/SliderView/README.md
Explains Xamarin.Forms Android Image Memory Management so could help you get around the issue you might be having.

Resource not found exception at setDropDownBackgroundColor

I'm trying to set a DropDown Background Color for a autocompleteTextView, because the standart Values seem to be different during different Android Versions. For example, everything works fine starting with android 3.2, but previous Versions seem to not accept the android:textColor="#color/black" in the xml, cause it is not working for me. Android 2.3 for example just shows the items in the list and the ones I picked in white, which isn't working for me, cause the background is white too. So I decided to change the background Color determing on the android version using this:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
// Do something for Honeycomb and above versions
//everything is right
} else{
// do something for phones running an SDK before froyo
box_Kurs.setDropDownBackgroundResource(Color.BLUE);
box_Teacher.setDropDownBackgroundResource(Color.BLUE);
}
But when I start my app it crashes and shows resource not found exception:
11-19 15:40:17.225: E/AndroidRuntime(450): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shr.khg/com.shr.khg.AddActivity}: android.content.res.Resources$NotFoundException: Resource ID #0xff0000ff
What am I doing wrong? I alread cleaned my project since that what other people suggested. Or is there any easier way to deal with my issue?
Thank you!
The problem is that the setDropDownBackgroundResource() is expecting the id of the Color resource Drawable, not an int that defines a Color. If you look at the Exception you can see that Android is trying to find a component with id #0xff0000ff, this is the hex code for a completely opaque blue which is the value of Color.BLUE
Replce these two lines:
box_Kurs.setDropDownBackgroundResource(Color.parseColor("#0000ff"));
box_Teacher.setDropDownBackgroundResource(Color.parseColor("#0000ff"));

Categories

Resources