我得到了
kotlin.UninitializedPropertyAccessException:lateinit 属性 BTNStatus05 尚未初始化
当我尝试设置 ButtonColor 时。 这个想法是根据通过 TCPsocket 的回复来更改按钮上的按钮颜色等。 在我的
我的主要活动简化:
class MainActivity() : AppCompatActivity(), Parcelable {
constructor(parcel: Parcel) : this() {
}
private var fgm: SygicNaviFragment? = null
private var uiInitialized = false
private val TAG = "MainActivity"
private lateinit var socketHandler: SocketHandler
private lateinit var handlerThread: HandlerThread
lateinit var BTNStatus05 : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (PermissionsUtils.requestStartupPermissions(this) == PackageManager.PERMISSION_GRANTED) {
checkSygicResources()
Log.i(TAG, "onCreate: Sygic Resources checked")
}
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.decorView.apply {
systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
}
setContentView(R.layout.activity_main)
handlerThread = HandlerThread("").apply {
start()
socketHandler = SocketHandler(looper)
socketHandler?.obtainMessage()?.also { msg ->
msg.what = 0
msg.obj = "INIT"
msg.arg1 = 0
socketHandler?.sendMessage(msg)}
}
// set on-click listener
BTNStatus05= findViewById(R.id.statusbutton05) as Button
Log.d(TAG, "BTN05" + BTNStatus05)
//BTNStatus05.setBackgroundColor(Color.YELLOW)
//BTNStatus05.setEnabled(false)
BTNStatus05.setOnClickListener {
socketHandler?.obtainMessage()?.also { msg ->
msg.what = 1
msg.obj = "btnpress"
msg.arg1 = 5
socketHandler?.sendMessage(msg)
}
Toast.makeText(this@MainActivity, "Status05 Clicked.", Toast.LENGTH_SHORT).show()
}
fun Test() {
Log.d(TAG, "DATA on Mainthread? " + Thread.currentThread().name)
Log.d(TAG, "Stop: We Got a button:")
BTNStatus05.setBackgroundColor(Color.YELLOW)
BTNStatus05.setEnabled(false)
}
override fun onCreateDialog(id: Int): Dialog {
return fgm?.onCreateDialog(id) ?: return super.onCreateDialog(id)
}
override fun onPrepareDialog(id: Int, dialog: Dialog) {
super.onPrepareDialog(id, dialog)
fgm?.onPrepareDialog(id, dialog)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
fgm!!.onNewIntent(intent)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
fgm?.onActivityResult(requestCode, resultCode, data)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return fgm?.onKeyDown(keyCode, event) ?: return super.onKeyDown(keyCode, event)
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
return fgm?.onKeyUp(keyCode, event) ?: return super.onKeyUp(keyCode, event)
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<MainActivity> {
override fun createFromParcel(parcel: Parcel): MainActivity {
return MainActivity(parcel)
}
override fun newArray(size: Int): Array<MainActivity?> {
return arrayOfNulls(size)
}
}
inner class MainHandler(mainlooper: Looper) : Handler(mainlooper) {
private val TAG = "MainActivity"
val mainA = MainActivity()
val mapper = jacksonObjectMapper()
override fun handleMessage(msg: Message) {
Log.d(TAG, "Handle")
super.handleMessage(msg)
val jsonstr = msg?.obj as String
val jsontree = mapper.readTree(jsonstr)
val type = jsontree.get("type").asText()
Log.d(TAG, "Mainhandler: jsonstr - " + jsonstr)
Log.d(TAG, "MainHandler:Json type - " + type )
when(type) {
"button" -> {
Log.d(TAG, "We got button update: " + type)
MainActivity().runOnUiThread(Runnable {
BTNStatus05.setBackgroundColor(Color.YELLOW) })
//BTNStatus05.setBackgroundColor(Color.YELLOW)
}
else -> Log.d(TAG, "unknown what " + type)
}
}
}
}
我尝试过:
when(type) {
"button" -> {
Log.d(TAG, "We got button update: " + type)
MainActivity().runOnUiThread(Runnable {
BTNStatus05.setBackgroundColor(Color.YELLOW) })
//BTNStatus05.setBackgroundColor(Color.YELLOW)
}
else -> Log.d(TAG, "unknown what " + type)
而且也只是打电话给
test()
...有什么想法吗?
kotlin.UninitializedPropertyAccessException: lateinit property BTNStatus05 has not been initialized
此异常要求您的代码正在访问声明为
lateinit
并且尚未初始化的属性。因此,当您将 BTNStatus05
声明为 lateinit
时,您必须在代码上的任何其他行访问它之前对其进行初始化。在您的情况下,当 MainHandler
调用 BTNStatus05.setBackgroundColor(Color.YELLOW)
时,BTNStatus05
未初始化。
要解决此问题,只需将按钮
BTNStatus05= findViewById(R.id.statusbutton05) as Button
的这一行初始化移动到 onCreate
方法的第一行内,如下所示。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
BTNStatus05= findViewById(R.id.statusbutton05)
// Other code
}
这将在访问之前初始化您的按钮,并且不会抛出相同的异常。