首页 > 解决方案 > 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>

标签: androidkotlin

解决方案


在这种情况下,您需要将可绘制对象的资源 id 传递给您的imageView.setImageResource. 在您的情况下,您没有传递可绘制对象的资源 id,而是为数组项执行此操作。

您可以在此处查看setImageResource 的工作原理。


推荐阅读