gpt4 book ai didi

java - 如何修复MainActivity? kotlin.UninitializedPropertyAccessException : lateinit property compositeDiposable has not been initialized

转载 作者:行者123 更新时间:2023-12-02 10:00:30 26 4
gpt4 key购买 nike

我收到的错误是“java.lang.RuntimeException:无法启动 Activity ComponentInfo {com.ginem.rxjavaandretrofit2demo/com.ginem.rxjavaandretrofit2demo.MainActivity}:kotlin.UninitializedPropertyAccessException:lateinit属性compositeDiposable尚未初始化”这是在应用程序完成并尝试在我的 Android 虚拟机中启动时发生的。

我正在创建一个andorid应用程序,它从假的REST Api中提取数据并使用RxJava和Retrofit2以cardview/recyclerview格式显示数据。

到目前为止,我已经尝试将依赖版本更改为较低版本,但仍然失败。我还添加了列出的所有 kotlin 插件:

应用插件:'kotlin-android'

应用插件:'kotlin-android-extensions'

应用插件:'kotlin-kapt'

package com.ginem.rxjavaandretrofit2demo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.ginem.rxjavaandretrofit2demo.Adapter.PostAdapter
import com.ginem.rxjavaandretrofit2demo.Model.Post
import com.ginem.rxjavaandretrofit2demo.Retrofit.IMyAPI
import com.ginem.rxjavaandretrofit2demo.Retrofit.RetrofitClient
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

internal lateinit var jsonApi:IMyAPI
internal lateinit var compositeDiposable: CompositeDisposable

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


// init API
val retrofit = RetrofitClient.instance
jsonApi = retrofit.create(IMyAPI::class.java)

//View
recycler_posts.setHasFixedSize(true)
recycler_posts.layoutManager=LinearLayoutManager(this)
fetchData()
}

private fun fetchData() {
compositeDiposable.add(jsonApi.posts
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe{posts->displayData(posts)})
}

private fun displayData(posts: List<Post>?) {

val adapter = PostAdapter(this, posts!!)
recycler_posts.adapter = adapter
}

}

模型:应用程序(build.gradle)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ginem.rxjavaandretrofit2demo"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-
jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//Retrofit2
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

//RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'


//Support
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'

}

IMyAPI接口(interface)

package com.ginem.rxjavaandretrofit2demo.Retrofit

import com.ginem.rxjavaandretrofit2demo.Model.Post
import io.reactivex.Observable
import retrofit2.http.GET

interface IMyAPI {

@get: GET("posts")
val posts: Observable<List<Post>>
}

RetrofitClient 类

package com.ginem.rxjavaandretrofit2demo.Retrofit

import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {

private var ourInstance: Retrofit? = null

val instance:Retrofit
get() {
if(ourInstance == null){
ourInstance = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
return ourInstance!!
}
}

最佳答案

问题正是错误消息所说的。在调用 add() 之前,我没有看到在任何地方实际设置 compositeDisposable 。那时它仍然为空。您需要使用compositeDisposable = ...对其进行初始化,更改为输入参数,使用依赖注入(inject)框架。

关于java - 如何修复MainActivity? kotlin.UninitializedPropertyAccessException : lateinit property compositeDiposable has not been initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com