Android运行时异常(应用无法打开)

时间:2018-03-11 06:22:59

标签: android firebase kotlin

  

E / AndroidRuntime:FATAL EXCEPTION:main

     

流程:za.co.riggaroo.motioncamera,PID:17583                                                                                kotlin.TypeCastException:null无法强制转换为非null类型kotlin.Boolean                                                                                    在za.co.riggaroo.motioncamera.MainActivity $ setupArmSystemToggle $ 2.onDataChange(MainActivity.kt:42)                                                                                    在com.google.android.gms.internal.to.zza(未知来源)                                                                                    在com.google.android.gms.internal.vj.zzHX(未知来源)                                                                                    在com.google.android.gms.internal.vp.run(未知来源)                                                                                    在android.os.Handler.handleCallback(Handler.java:751)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:154)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6776)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1496)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

package za.co.riggaroo.motioncamera

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.SwitchCompat
import android.util.Log
import android.widget.ImageView
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener

class MainActivity : AppCompatActivity() {

private lateinit var adapter: LogsAdapter
private lateinit var armSystemToggleButton: SwitchCompat
private lateinit var armSystemImageView: ImageView

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

    setupMotionLogsRecyclerView()
    setupArmSystemToggle()
}

private fun setupArmSystemToggle() {
    armSystemToggleButton = findViewById(R.id.switch_arm_system)
    armSystemImageView = findViewById(R.id.image_view_arm_system)
    val armedValue =
            FirebaseDatabase.getInstance().getReference(SYSTEM_ARMED_STATUS_FIREBASE_REF)

    armSystemToggleButton.setOnCheckedChangeListener { _, checkedValue ->
        armedValue.setValue(checkedValue)
    }

    armedValue.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            Log.d(ACT_TAG, "onDataChange:" + dataSnapshot.toString())
            val isArmed = dataSnapshot.value as Boolean
            toggleUIState(isArmed)
        }

        override fun onCancelled(p0: DatabaseError?) {

        }

    })
}

private fun toggleUIState(isArmed: Boolean) {
    armSystemToggleButton.isChecked = isArmed
    armSystemToggleButton.text = if (isArmed) {
        getString(R.string.system_armed)
    } else {
        getString(R.string.system_unarmed)
    }
    val armedImageResource = if (isArmed) {
        R.drawable.ic_armed
    } else {
        R.drawable.ic_not_armed
    }
    armSystemImageView.setImageResource(armedImageResource)

}

private fun setupMotionLogsRecyclerView() {
    val recyclerViewImages = findViewById<RecyclerView>(R.id.recyclerViewImages)
    recyclerViewImages.isNestedScrollingEnabled = false
    val databaseRef = FirebaseDatabase.getInstance().getReference(MOTION_LOGS_FIREBASE_REF)

    adapter = LogsAdapter(databaseRef.orderByChild(ORDER_BY_TIMESTAMP).ref)
    val linearLayoutManager = LinearLayoutManager(this)
    linearLayoutManager.reverseLayout = true
    linearLayoutManager.stackFromEnd = true
    recyclerViewImages.layoutManager = linearLayoutManager
    recyclerViewImages.adapter = adapter
}

override fun onDestroy() {
    super.onDestroy()
    adapter.cleanup()
}

companion object {
    private const val ORDER_BY_TIMESTAMP = "timestamp"
    private const val ACT_TAG: String = "MainActivity"
    private const val MOTION_LOGS_FIREBASE_REF = "motion-logs"
    private const val SYSTEM_ARMED_STATUS_FIREBASE_REF = "system-armed"
}


}

2 个答案:

答案 0 :(得分:2)

请参考,

kotlin.TypeCastException: null cannot be cast to non-null type com.midsizemango.databasekotlin.Note

在你的情况下试试这个:

val isArmed = dataSnapshot.value as? Boolean

答案 1 :(得分:0)

onDataChange(..)

中试试
     if(dataSnapshot.value != null) {          
          val isArmed = dataSnapshot.value as Boolean
          toggleUIState(isArmed)
     }