android - android 应用程序在启动时崩溃(致命异常,没有为 ID 0xffffffff 找到包 ID ff)
问题描述
因此,每当我尝试启动我的应用程序时,它都会立即崩溃。日志中的错误是:
2021-04-21 13:18:57.147 5707-5707/com.example.myapp E/com.example.myapp: No package ID ff found for ID 0xffffffff.
2021-04-21 13:18:57.148 5707-5707/com.example.myapp D/AndroidRuntime: Shutting down VM
2021-04-21 13:18:57.153 5707-5707/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 5707
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:237)
at android.content.res.Resources.getValue(Resources.java:1428)
at androidx.appcompat.widget.ResourceManagerInternal.createDrawableIfNeeded(ResourceManagerInternal.java:176)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:141)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:132)
at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:90)
at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:98)
at com.example.myapp.MainActivity.onCreate(MainActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
它与我的代码有关,MainActivity
但我不知道出了什么问题,因为它实际上在一段时间前有效,然后出现了这个错误。
主要活动
package com.example.myapp
import android.content.res.TypedArray
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val images: TypedArray = resources.obtainTypedArray(R.array.images)
val imageView1 = findViewById<ImageView>(R.id.imageView1)
val imageView2 = findViewById<ImageView>(R.id.imageView2)
val imageView3 = findViewById<ImageView>(R.id.imageView3)
val imageView4 = findViewById<ImageView>(R.id.imageView4)
val imageView5 = findViewById<ImageView>(R.id.imageView5)
val imageView6 = findViewById<ImageView>(R.id.imageView6)
imageView1.setImageResource(images.getResourceId(Random.nextInt(20000), -1))
imageView2.setImageResource(images.getResourceId(Random.nextInt(20000), -1))
imageView3.setImageResource(images.getResourceId(Random.nextInt(20000), -1))
imageView4.setImageResource(images.getResourceId(Random.nextInt(20000), -1))
imageView5.setImageResource(images.getResourceId(Random.nextInt(20000), -1))
imageView6.setImageResource(images.getResourceId(Random.nextInt(20000), -1))
}
}
数组.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="images">
<item>image1</item>
<item>image2</item>
...
<item>image20000</item>
</array>
</resources>
解决方案
在这种情况下,您需要将可绘制对象的资源 id 传递给您的imageView.setImageResource
. 在您的情况下,您没有传递可绘制对象的资源 id,而是为数组项执行此操作。
您可以在此处查看setImageResource 的工作原理。
推荐阅读
- javascript - 条件未正确解决
- authentication - 在密码中使用字符 EURO 时,使用 LDAP(针对 Active Directory)的 Tomcat 身份验证失败
- python - 如何在 python 中仅对某些模型变量执行部分 F 测试
- angular - Angular 在组件中无法使用某些样式?
- javascript - discord bot帮助命令没有做任何事情
- mongodb - 使用 mongoDB 作为源时,AWS DMS 不会将 int 升级为 double
- javascript - 正则表达式允许 # 仅在字符串中的字符之间
- c# - Moq 和 xUnit 的单元测试失败 - InvalidOperationException
- reactjs - 如何“模仿”与 React Firebase 云的一对多关系?
- javascript - 碰撞检测html5画布