javaandroid-studiokotlindrawablelayer-list

how to reference an item from layer-list.xml in activity


When I try to run my app I get an error that says "Attempt to invoke virtual method on a null object reference" , that's occuring because I was trying to access items from a layer-list through main activity and I probably referenced them incorrectly which leads me here.

I set the individual items from my layer-list.xml to variables, they were found by their ids. My item variables have all been returning as null. Below is my code:






import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.ImageView


import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.helper.widget.Layer



class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


Log.d("EA","My array list of drawables is created next")
        val hairList = arrayListOf(R.drawable.hairponytailblue , R.drawable.hairponytailpink)
        val mouthList = arrayListOf(R.drawable.smilebigsmile, R.drawable.smileopenmouthfrown)
        val backgroundImageList = arrayListOf(R.drawable.background , R.drawable.background2)
Log.d("XX","done")


Log.d("EA","created the list iterators")
        var hairListIterator = hairList.iterator()
        var mouthListIterator = mouthList.iterator()
        var backgroundImageListIterator = backgroundImageList.iterator()

        Log.d("XX","done")


Log.d("EA","Below I created the variable for the images and buttons in use here, I also set my main images image resource to my layer-list.xml")

      //everything seems "fine" at this declaration but when I hover over R.drawable.layer , it returns its exact path on my computer in bright red, below that it says "@drawable/layer =>layer.xml"
      //
       val basePicture = findViewById<ImageView>(R.id.mainimage).setImageResource(R.drawable.layer)


        val hairButton = findViewById<Button>(R.id.hairbutton)
        val mouthButton = findViewById<Button>(R.id.mouthbutton)
        val backgroundButton = findViewById<Button>(R.id.backgroundbutton)

Log.d("XX","done")

 Log.d("EA","Here I set up the items/layers in layer.xml that I want to change")
        //these keep returning as null in debug but program continues on , I think its because of HOW they were referenced, these are items INSIDE a layer-list.xml.

        val hairLayer = findViewById<Layer>(R.id.layerHair)
        val mouthLayer = findViewById<Layer>(R.id.layerMouth)
        val backgroundLayer = findViewById<Layer>(R.id.layerBackground)

 Log.d("XX","Done")

 Log.d("EA", "Here I set the first of my arrays as each layers first images")

       /*error starts here,
       my error says : "Attempt to invoke virtual method 'void androidx.constraintlayout.helper.widget.Layer.setBackgroundResource(int)' on a null object reference "
       this has something to do with hairLayer...*/

        hairLayer.setBackgroundResource(hairList.first())
        mouthLayer.setBackgroundResource(mouthList.first())
        backgroundLayer.setBackgroundResource(backgroundImageList.first())

Log.d("XX","Done")

Log.d("EA","Here I told my list iterators to do something")

        hairButton.setOnClickListener {
            if( hairListIterator.hasNext()){
                hairLayer.setBackgroundResource(hairListIterator.next())

            }
            else{
                hairLayer.setBackgroundResource(hairList.first())
                hairListIterator = hairList.iterator()
            }
        }
Log.d("EA2","hairLayers new background resource is : " + hairLayer.drawableState)

Log.d("XX","done")






    }
}

my goal with these Layer-list items, is to change their drawables. I found ways to do this - like "mutate()" and "setDrawableByLayerId()" - but they all have to do with LayerDrawable. According to the AndroidStudio Developer website, a layer-list compiles a layerdrawable? I'm pretty confused there.

p.s. if my layer-list is not already a LayerDrawable, how would I convert it to one? I feel like if I could convert my Layer-List to a layerDrawable somewhere at the beginning of the code, then I wouldn't have to constantly try to reference individual items inside of Layer-list.xml.


Solution

  • A layer-list is converted into a LayerDrawable object at runtime, i.e. it is the same thing.

    It is impossible to pull an item directly from a LayerDrawable by id. This is how it works (Java example):

    LayerDrawable layers = (LayerDrawable) ContextCompat.getDrawable( this, R.drawable.layer-list ); // assuming 'layer-list' is indeed your filename.
    
    List<Drawable> hairList = Arrays.asList( 
        new Drawable[]{layers.getDrawable( layers.findIndexById( R.id.hairponytailblue ) ),
                       layers.getDrawable( layers.findIndexById( R.id.hairponytailpink ) ) 
    });