android / codelab-android-dynamic-features

Apache License 2.0
123 stars 47 forks source link

android.content.res.Resources$NotFoundException: Resource ID #0x0 #23

Open Shivamgarg1528 opened 4 years ago

Shivamgarg1528 commented 4 years ago

Hi,

Post installation of dynamic module, if we are accessing any resource through Resources#getIdentifier(String name, String defType, String defPackage) method which returns 0 as resourceId

We have added only one resource in our drawable folder i.e default_refer.png and made some changes in KotlinSampleActivity.

Kindly suggest what we are doing wrong.

class KotlinSampleActivity : BaseSplitActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feature_kotlin)

        // not working
        try {
            ContextCompat.getDrawable(this, resources.getIdentifier("default_refer", "drawable", packageName))
        } catch (e: Exception) {
            e.printStackTrace()
        }

        // working
        try {
            ContextCompat.getDrawable(this, R.drawable::class.java.getField("default_refer").getInt(null))
        } catch (e: Exception) {
            e.printStackTrace()
        }

        // working
        try {
            ContextCompat.getDrawable(this, R.drawable.default_refer)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
keyboardsurfer commented 4 years ago

It seems like your issue is related to the packageName. Please check that the resource package name is correct for your first call.