android - 带有对象问题的 Kotlin 单例
问题描述
我正在尝试用 Kotlin 做单例模式,但我不明白为什么我在这里得到空指针异常
object AppController : Application() {
private val queue: RequestQueue = Volley.newRequestQueue(applicationContext)
fun getRequestQueue(): RequestQueue {
return queue
}
}
在我的主要活动中,我打电话给:
private val controller = AppController
private val queue = AppController.getRequestQueue()
感谢您的帮助。对不起。我不确定为什么代码格式不正确。
解决方案
只需将对象更改为类,可能android初始化它有问题
class AppController : Application() {
private val queue: RequestQueue = Volley.newRequestQueue(applicationContext)
fun getRequestQueue(): RequestQueue {
return queue
}
}
干杯
推荐阅读
- laravel - Laravel Livewire:输入选择,选择默认选项
- string - 如何从 SVG 字符串中预缓存 SVG 图像?
- parameters - Azure DataFactory 中的当前日期参数
- swift - iOS:自定义密码作为生物识别的后备
- performance - Xamarin - 性能 - 大量标签
- php - 引用破坏 json 编码
- c# - C#中for循环的问题
- makefile - Makefile 先决条件中的变量赋值
- reactjs - 如何从打字稿 useReducer 挂钩安全地断言某个状态?
- python - BigQuery 的 IPython Magics - 使用参数不起作用