当我尝试运行我的应用程序时,我得到一个错误,上面写着“尝试在空对象引用上调用虚拟方法”,这是因为我试图通过主活动访问层列表中的项,并且我可能错误地引用了它们,这导致了我出现在这里。
我将layer-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、 如果我的图层列表还不是一个layerdrawable,我该如何将它转换成一个呢?我觉得如果我可以在代码开头的某个地方将我的层列表转换为layerdrawable,那么我就不必不断地尝试引用layer-list.xml中的单个项。
1条答案
按热度按时间qjp7pelc1#
一
layer-list
转换为LayerDrawable
对象,即它是相同的东西。不可能拉一根绳子
item
直接从LayerDrawable
由id
. 工作原理如下(java示例):