Kotlin android.content.res.Resources$NotFoundException:资源 ID #0x7f07006e

问题描述 投票:0回答:1

尝试“注册”后,kotlin 应用程序会崩溃,怀疑它无法找到或读取可绘制文件夹中的图像,从而出现问题。我尝试重命名图像,但问题仍然存在,尽管 logcat 上显示错误,但 android studio 上似乎没有显示错误。任何帮助将不胜感激:)

MainActivity.kt

package com.example.lightning

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.lightning.data.Location
import com.example.lightning.data.Profile
import com.example.lightning.ui.theme.LightningTheme
import com.example.lightning.ui2.ProfileCard
import com.example.lightning.ui2.login.EmailLoginScreen
import com.example.lightning.ui2.login.LoginScreen
import androidx.compose.foundation.lazy.LazyColumn
import com.example.lightning.ui2.login.RegistrationScreen

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LightningTheme {
                val navController = rememberNavController()
                NavHost(navController = navController, startDestination = "login_screen") {
                    composable("login_screen") { LoginScreen(navController) }
                    composable("email_login_screen") { EmailLoginScreen(navController) }
                    composable("profile_screen") { ProfileScreen(getDummyProfileData()) }
                    composable("registration_screen") { RegistrationScreen(navController) }
                }
            }
        }
    }

    // Function to get sample profile data (replace with your data source)
    private fun getDummyProfileData(): List<Profile> {
        return listOf(
            Profile(
                id = "1",
                name = "Alice Johnson",
                age = 28,
                imageUrl = "https://via.placeholder.com/150", // Placeholder image URL
                bio = "Loves hiking, traveling, and trying new foods.",
                gender = "Female",
                interests = listOf("Hiking", "Traveling", "Cooking"),
                location = Location("San Francisco", "California", "USA"),
                occupation = "Software Engineer",
                education = "Master's Degree",
                relationshipStatus = "Single",
                lookingFor = "Serious relationship"
            ),
            Profile(
                id = "2",
                name = "Bob Smith",
                age = 32,
                imageUrl = "https://via.placeholder.com/150",
                bio = "Enjoys playing guitar, watching movies, and exploring new places.",
                gender = "Male",
                interests = listOf("Music", "Movies", "Travel"),
                location = Location("New York", "New York", "USA"),
                occupation = "Musician",
                education = "Bachelor's Degree",
                relationshipStatus = "Single",
                lookingFor = "Casual dating"
            ),
            // Add more dummy profiles here...
        )
    }

    @Composable
    fun ProfileScreen(profiles: List<Profile>) {
        LightningTheme {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                LazyColumn {
                    items(profiles.size) { index ->
                        ProfileCard(profiles[index])
                    }
                }
            }
        }
    }
}


个人资料卡.kt

package com.example.lightning.ui2 // Adjust if your package is different

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.lightning.R
import com.example.lightning.data.Profile

@Composable
fun ProfileCard(profile: Profile) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
        colors = CardDefaults.cardColors(Color(0xFF000000)),
    ) {
        Row(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            //Profile Image
            AsyncImage(
                model = profile.imageUrl,
                contentDescription = "Profile Image",
                placeholder = painterResource(id = R.drawable.error_image),
                error = painterResource(id = R.drawable.placeholder_image),
                modifier = Modifier
                    .size(80.dp)
                    .clip(CircleShape),
                contentScale = ContentScale.Crop
            )

            Spacer(modifier = Modifier.width(16.dp))

            Column {
                Text(text = profile.name, style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.Bold, color = Color.White)
                Text(text = "${profile.age}", style = MaterialTheme.typography.bodyMedium, color = Color.White)
                // Add more Text composables for other profile details
            }
        }
    }
}

Logcat日志

FATAL EXCEPTION: main
Process: com.example.lightning, PID: 8673
android.content.res.Resources$NotFoundException: Resource ID #0x7f07006e
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:240)
    at android.content.res.Resources.getValue(Resources.java:1511)
    at androidx.compose.ui.res.PainterResources_androidKt.painterResource(PainterResources.android.kt:61)
    at com.example.lightning.ui2.ProfileCardKt$ProfileCard$1.invoke(ProfileCard.kt:39)
    at com.example.lightning.ui2.ProfileCardKt$ProfileCard$1.invoke(ProfileCard.kt:27)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
    at androidx.compose.material3.CardKt$Card$1.invoke(Card.kt:886)
    at androidx.compose.material3.CardKt$Card$1.invoke(Card.kt:93)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
    at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:134)
    at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:115)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
    at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
    at androidx.compose.material3.SurfaceKt.Surface-T9BRK9s(Surface.kt:112)
    at androidx.compose.material3.CardKt.Card(Card.kt:85)
    at com.example.lightning.ui2.ProfileCardKt.ProfileCard(ProfileCard.kt:21)
    at com.example.lightning.MainActivity$ProfileScreen$1$1$1$1.invoke(MainActivity.kt:83)
    at com.example.lightning.MainActivity$ProfileScreen$1$1$1$1.invoke(MainActivity.kt:82)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:139)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
    at androidx.compose.foundation.lazy.LazyListItemProviderImpl$Item$1.invoke(LazyListItemProvider.kt:79)
    at androidx.compose.foundation.lazy.LazyListItemProviderImpl$Item$1.invoke(LazyListItemProvider.kt:77)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
    at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:248)
    at androidx.compose.foundation.lazy.layout.LazyLayoutPinnableItemKt.LazyLayoutPinnableItem(LazyLayoutPinnableItem.kt:58)
    at androidx.compose.foundation.lazy.LazyListItemProviderImpl.Item(LazyListItemProvider.kt:77)
    at androidx.compose.foundation.lazy.layout.LazyLayoutItemContentFactoryKt$SkippableItem$1.invoke(LazyLayoutItemContentFactory.kt:135)
    at androidx.compose.foundation.lazy.layout.LazyLayoutItemContentFactoryKt$SkippableItem$1.invoke(LazyLayoutItemContentFactory.kt:134)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
    at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
    at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:248)
    at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84)
    at androidx.compose.foundation.lazy.layout.LazySaveableStateHolder.SaveableStateProvider(LazySaveableStateHolder.kt:85)
    at androidx.compose.foundation.lazy.layout.LazyLayoutItemContentFactoryKt.SkippableItem-JVlU9Rs(LazyLayoutItemContentFactory.kt:134)
    at androidx.compose.foundation.lazy.layout.LazyLayoutItemContentFactoryKt.access$SkippableItem-JVlU9Rs(LazyLayoutItemContentFactory.kt:1)
    at androidx.compose.foundation.lazy.layout.LazyLayoutItemContentFactory$CachedItemContent$createContentLambda$1.invoke(LazyLayoutItemContentFactory.kt:101)

采取的步骤 -

  1. 清理和重建构建
  2. 尝试执行文件 -> 使缓存无效/重新启动,然后重新启动 Android Studio。
  3. val resourceId = resources.getIdentifier("my_resource_name", "id", packageName)
    添加到 MainActivity.kt 并重建,但问题仍然存在。
android kotlin android-studio fragment kotlin-coroutines
1个回答
0
投票

设法解决了我自己的疑问。发现受影响的图像之一存储在不同的可绘制文件夹中。移动到正确的文件夹后,错误得到修复。请参阅下面的相关图片。谢谢大家的建议:)

[初始图像位置][1] [1]:https://i.sstatic.net/Vjijg3th.png

[更新了图像位置][2] [2]:https://i.sstatic.net/51SJMLLH.png

© www.soinside.com 2019 - 2024. All rights reserved.