如何在 activity 中引用图层-list.xml 中的项目

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

当我尝试 运行 我的应用程序时,我收到一条错误消息,显示 "Attempt to invoke virtual method on a null object reference",这是因为我试图通过 main activity 访问图层列表中的项目,我可能错误地引用了它们,这导致我来到这里。

我将图层-list.xml 中的各个项目设置为变量,它们是通过它们的 id 找到的。我的项目变量都返回为 null。下面是我的代码:






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")






    }
}

我对这些图层列表项的目标是更改它们的可绘制对象。我找到了执行此操作的方法——例如“mutate()”和“setDrawableByLayerId()”——但它们都与 LayerDrawable 有关。根据 AndroidStudio 开发者网站,层列表编译 layerdrawable?我在那里很困惑。

p.s。如果我的图层列表还不是 LayerDrawable,我如何将其转换为一个?我觉得如果我可以在代码开头的某处将我的 Layer-List 转换为 layerDrawable,那么我就不必经常尝试引用 Layer-list.xml.

中的各个项目

一个layer-list在运行时被转换成一个LayerDrawable对象,即是同一个东西。

不可能通过 idLayerDrawable 直接拉出 item。这是它的工作原理(Java 示例):

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 ) ) 
});