Context.resources.getIdentifier löst eine „resourcenotfoundException“ ausAndroid

Forum für diejenigen, die für Android programmieren
Guest
 Context.resources.getIdentifier löst eine „resourcenotfoundException“ aus

Post by Guest »

Ich verwende

Code: Select all

  val name = if (!formula.productName?.nameRes.isNullOrBlank()) {
stringResource(getStringResId(formula.productName?.nameRes!!))
} else if (!formula.productName?.name.isNullOrBlank()) {
formula.productName?.name
} else {
stringResource(R.string.common_empty_string)
}
Text(
text = name!!,
fontSize = textSize.nsp(),
color = Color.White,
textAlign = TextAlign.Center,
modifier = Modifier
.align(alignment = Alignment.TopCenter)
.offset(y = 124.dp)
)

@Composable
fun getImageResId(imageName: String): Int {
val context = LocalContext.current
return context.resources.getIdentifier(imageName, "drawable", context.packageName)
}

@Composable
fun getStringResId(stringName: String): Int {
val context = LocalContext.current
return context.resources.getIdentifier(stringName, "string", context.packageName)
}
um eine Ressource zu finden.
aber wenn ich zwei Klassen aus meinem Projekt entferne, wird es diese Ausnahme verursachen

Code: Select all

 android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:444)
at android.content.res.Resources.getString(Resources.java:537)
at
androidx.compose.ui.res.StringResources_androidKt.stringResource(Unknown Source:20)

Das sind diese beiden Klassen. Ich bin mir ziemlich sicher, dass sie nicht von anderen Klassen referenziert werden. aber wenn ich sie wieder in mein Projekt einbaue, läuft es gut.

Code: Select all

@Immutable
data class DrinksModel(
val productId: Int,
var type: ProductType,
@StringRes val name: Int,
@DrawableRes val imageRes: Int,
)

@Singleton
class HomeLocalDataSource @Inject constructor() {
val specialItem = mutableListOf(1, 3, 4, 11, 12)

val drinksTypes = listOf(
DrinksModel(
productId = 1,
type = ProductType.STEAM,
name = R.string.home_item_stop,
imageRes = R.drawable.home_stop_normal_ic
),
...
)
}
das sind meine Projekteinstellungen

Code: Select all

[versions]
agp = "8.4.0"
kotlin = "1.9.24"
ksp = "1.9.24-1.0.20"
coreKtx = "1.13.1"
hilt = "2.51.1"
Ich möchte nur wissen, warum meine App eine „ResourcenotfoundException“ erhält, wenn ich diese beiden Klassen entferne.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post