当前位置: 首页 > 知识库问答 >
问题:

我有一个lateinit错误“Kotlin.UninitializedPropertyAccessException:lateinit属性对话框没有被初始化”

容学林
2023-03-14

我在这段代码中得到了一条消息“kotlin.uninitializedPropertyAccessException:lateinit property dialog没有被初始化”

  class AlarmClock : Application() {
      var serviceBinder: AlarmService.AlarmBinder? = null
  
      override fun onCreate() {
          super.onCreate()
          instance = this
          Util.ringtones += Util.getRingtones(this)

          val si = Intent(this, AlarmService::class.java).apply {
              action = Constants.ActionInit
          }

          bindService(si, Connection(this), Context.BIND_AUTO_CREATE or Context.BIND_ABOVE_CLIENT)
      }

      class Connection(val parent: AlarmClock) : ServiceConnection {
          override fun onServiceDisconnected(name: ComponentName?) {
              parent.serviceBinder = null
          }

          override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
              parent.serviceBinder = service as? AlarmService.AlarmBinder
              parent.serviceBinder?.also { binder ->
                  ServiceListeners.forEach { it(binder) }
              }
          }
      }

      fun doWithService(func: (AlarmService.AlarmBinder) -> Unit) {
          if (serviceBinder != null) {
              func(serviceBinder!!)
          } else {
              ServiceListeners += func
          }
      }

      companion object {
          val gson = Gson()
          val ServiceListeners = mutableListOf<(AlarmService.AlarmBinder) -> Unit>()
          **lateinit var instance: AlarmClock**
              private set
      }
  }
 class AlarmService : Service(), SensorEventListener {

     private lateinit var preferences: SharedPreferences

     val timer = Timer("AlarmClock")
     val alarms = mutableListOf<Alarm>()

     var current: Alarm? = null
     var mp: MediaPlayer? = null
     val handler = Handler()
     var sm: SensorManager? = null

     override fun onBind(intent: Intent): IBinder {
         preferences = applicationContext.getSharedPreferences(Constants.PreferencesAlarms,Context.MODE_PRIVATE)
         when (intent.action) {
             Constants.ActionInit -> {
                 val alarmList = preferences.getStringSet(Constants.AlarmList, emptySet())
                 alarms += alarmList.mapNotNull { gson.fromJson(preferences.getString(it, ""), Alarm::class.java) }

                 var c = Calendar.getInstance()
                 val delay = 60 - c.get(Calendar.SECOND)

                 timer.scheduleAtFixedRate(object : TimerTask() {
                     override fun run() {
                         c = Calendar.getInstance()
                         val h = c.get(Calendar.HOUR_OF_DAY)
                         val m = c.get(Calendar.MINUTE)
                         val d = c.get(Calendar.DAY_OF_WEEK)

                         alarms.filter { it.enabled }.forEach {
                             if (it.timeH == h && it.timeM == m && (it.activeOnDay(d) || !it.repeat.values.contains(true))) {
                                 val timestamp = SimpleDateFormat("mm-hh-dd-MM-yyyy", Locale.US)
                                 Log.i("WHAT THE FUCK", it.lastTime + "?=" + timestamp.format(Date()))
                                 if (it.lastTime != timestamp.format(Date())) {
                                     current = it
                                     startAlarm(it.id)
                                 }

                             }
                         }
                     }
                 }, delay * 1000L, 1000)
             }
         }

         return AlarmBinder(this)
     }

     override fun onDestroy() {
         super.onDestroy()
         sm?.unregisterListener(this)
     }

     fun startAlarm(id: String) {
         mp = MediaPlayer().also {
             it.isLooping = true
         }
         val alarm = gson.fromJson(preferences.getString(id, ""), Alarm::class.java)
         val volume = alarm.volume.toFloat() / 100

         try {
             mp?.setDataSource(this@AlarmService, Uri.parse(alarm.ringtoneUri))
             mp?.prepare()
             mp?.setVolume(volume, volume)
             mp?.start()
         } catch (e: IOException) {
             mp = null
         }

         if (current?.snoozeOnMove == true) {
             sm = getSystemService(Context.SENSOR_SERVICE) as SensorManager
             val accelerometer = sm?.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)

             sm?.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST)
         }

         val timestamp = SimpleDateFormat("mm-hh-dd-MM-yyyy", Locale.US)
         alarm.lastTime = timestamp.format(Date())

         if (!alarm.repeat.values.contains(true)) {
             alarm.enabled = false
         }
         val alarmData = AlarmClock.gson.toJson(alarm)
         preferences.edit().also {
             it.putString(id, alarmData)
         }.apply()
         alarms.removeAll { it.id == id }
         alarms += alarm

         val intent = Intent(this, AlarmActive::class.java)
         intent.putExtra(Constants.AlarmID, id)
         startActivity(intent)
     }

     class AlarmBinder(val service: AlarmService) : Binder() {
         var snoozeListener: SnoozeListener? = null

         fun refreshAlarms() {
             service.alarms.clear()
             val alarmList = service.preferences.getStringSet(Constants.AlarmList, emptySet())
             service.alarms += alarmList.mapNotNull { gson.fromJson(service.preferences.getString(it, ""), Alarm::class.java) }
         }

         fun snoozeAlarm() {
             if (service.current?.snoozeTime ?: 0 > 0) {
                 snoozeListener?.onSnooze()
                 service.mp?.stop()

                 service.handler.postDelayed({
                     service.mp?.start()
                 }, service.current?.snoozeTime!! * 60000L)
             }
         }

         fun stopAlarm() {
             service.current = null
             service.sm?.unregisterListener(service)
             service.mp?.stop()
         }
     }

     override fun onSensorChanged(event: SensorEvent) {
         if (event.sensor.type == Sensor.TYPE_LINEAR_ACCELERATION) {
             val x = event.values[0]
             val y = event.values[1]
             val z = event.values[2]

             val diff = Math.sqrt((x * x + y * y + z * z).toDouble())
                  if (diff > 0.5) {
                 **AlarmClock.instance.doWithService** {
                     it.snoozeAlarm()
                 }
             }
         }
     }

     interface SnoozeListener {
         fun onSnooze()
     }
 
     companion object {
         val gson = Gson()
     }

     override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
 }

你能检查出什么问题并教我该怎么做吗?

共有1个答案

晋坚
2023-03-14

这有点猜测。因为我看到您在alarmclock.oncreate()中设置了alarmclock.instance,所以该实例的设置应该很好,除非您忘记将该类设置为清单中的应用程序类。

元素中,应该放置Android:name=“.alarmclock”

 类似资料:
  • 我不知道这是怎么回事 主要活动 我遇到了这个问题 2022-03-18 09:18:27.393 13874-13874/com。实例githubuser2 E/AndroidRuntime:致命异常:主进程:com。实例githubuser2,PID:13874 java。lang.RuntimeException:无法启动活动组件信息{com.example.githubuser2/com.e

  • 我正在尝试使用Spring-boot+Vaadin创建一个web项目,并且希望使用spring-data-jpa和hibernate从PostgreSQL数据库中获取数据。 在我的Vaadin看来,我尝试自动连接我的服务类,但我总是得到null,而且eror堆栈跟踪没有告诉我原因。 数据库得我得配置: 视图和UI: pom.xml: 我的服务: 更新的解决方案:

  • 我有一个自动编码器,我尝试使用lambda在中间层中使用输入层的特定值,生成一个新的张量并发送到下一层,但它会产生以下错误: 回溯(最近一次呼叫最后一次): File"",第99行,在Model=Model(输入=[图像,wtm],输出=解码)中 文件“D:\software\Anaconda3\envs\py36\lib\site packages\keras\legacy\interfaces

  • 当我执行代码时,我得到一个错误, 属性错误:“WebDriver”对象没有属性“find_element_by_xpath”

  • 我试图传递IOUFlowIssueTest,但给我的错误是MockNetwork没有初始化,而它已经初始化了。 UninitializedPropertyAccessException:lateinit属性mockNetwork尚未初始化

  • 我正在为DI与Dagger2的项目工作。我正在MVP架构中注入presenter。由于某种原因,当我构建应用程序时,它会出现错误:“LateInit property presenter has not been initialized”。我知道这意味着没有注射,但我不明白为什么。下面是我的代码: 应用程序类 家庭模块 AppComponent 房屋结构 如果需要任何关于代码的其他信息,请询问..