You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin Android应用注册后崩溃,报Resources$NotFoundException求助

Kotlin Android应用点击注册后崩溃:Resources$NotFoundException

我的Kotlin Android应用点击“注册”后立即崩溃,报错android.content.res.Resources$NotFoundException: Resource ID #0x7f07006e。怀疑是找不到drawable文件夹里的图片,试过重命名图片、Clean Rebuild项目、Invalidate Caches/Restart Android Studio,还在MainActivity.kt里加了val resourceId = resources.getIdentifier("my_resource_name", "id", packageName),问题还是没解决。Android Studio没有报错提示,但Logcat显示上述错误。


相关代码

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])
                    }
                }
            }
        }
    }
}

ProfileCard.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)

问题定位与解决方案

从Logcat栈追踪可以明确,崩溃发生在ProfileCard.kt中调用painterResource加载drawable资源的位置,对应代码里的R.drawable.error_image或R.drawable.placeholder_image,系统无法找到对应的资源文件。

具体排查与修复步骤:

  1. 验证资源文件存在性

    • 检查res/drawable及各密度子文件夹(如drawable-hdpi)下,是否存在error_image和placeholder_image文件,文件名必须完全匹配(仅允许小写字母、数字、下划线、点,大小写敏感)。
    • 确认图片格式为Android支持的类型(PNG/JPG),且文件未损坏。
  2. 检查资源ID映射

    • 打开项目的build/generated/not_namespaced_r_class_sources/debug/r/com/example/lightning/R.java文件,搜索0x7f07006e,查看该ID对应的资源名称,确认是否为上述两个图片之一,以及是否在R类中正确生成。
  3. 修复资源引用错误

    • 若资源存在但R类未生成对应ID,删除项目根目录下的.idea文件夹和build文件夹,重新Sync项目并Rebuild。
    • 你之前使用getIdentifier时参数错误,drawable资源的类型参数应为"drawable"而非"id",正确写法:
      val resourceId = resources.getIdentifier("error_image", "drawable", packageName)
      
    • 可临时替换为系统内置资源(如R.drawable.ic_launcher_background)测试,若不再崩溃,说明是自定义资源的问题。
  4. 检查导航与页面逻辑

    • 点击注册后预期跳转RegistrationScreen,但崩溃栈显示在ProfileScreen中,需检查RegistrationScreen的实现,确认是否意外触发了ProfileScreen的加载,或在注册流程中错误调用了ProfileCard相关代码。

内容的提问来源于stack exchange,提问作者BRSV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 20:07:01