如何在活动中引用图层列表中.xml的项目



当我尝试运行我的应用程序时,我收到一个错误,说"尝试在空对象引用上调用虚拟方法">,这是因为我试图通过 main 活动从图层列表中访问项目,我可能错误地引用了它们,这导致我来到这里。

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




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 Developer网站,图层列表编译了layerdrawable?我在那里很困惑。

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

layer-list

在运行时被转换为LayerDrawable对象,即它是一回事。

不可能通过id直接从LayerDrawable中拉出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 ) ) 
});

最新更新