So whenever I try to start my app, it crashes immediately. The error in the log is:
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)
It has something to do with my code in MainActivity
but I don't know what's wrong because it actually worked a time ago and then this error showed up.
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))
}
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="images">
<item>image1</item>
<item>image2</item>
...
<item>image20000</item>
</array>
</resources>
In this case, you need to pass the resource id of a drawable to your imageView.setImageResource
. In your case you are not passing the resource id of your drawable, rather you are doing it for the array item.
You can check it here, as to how setImageResource works.
User contributions licensed under CC BY-SA 3.0