gpt4 book ai didi

android - lateinit property dispatchingAndroidInjector 还没有初始化

转载 作者:搜寻专家 更新时间:2023-11-01 07:42:26 25 4
gpt4 key购买 nike

当我在 dagger2 中运行我的 fragment 时,我收到上述错误 "lateinit property dispatchingAndroidInjector has not been initialized "
上面的错误是在我的

下面的应用程序类中触发的

KotlinTemplateApplication.kt

     class KotlinTemplateApplication:Application(), HasActivityInjector  {
lateinit var retroComponent:RetroComponent

@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

companion object {
@get:Synchronized
lateinit var instance: KotlinTemplateApplication
private set
}

override fun onCreate() {
super.onCreate()
instance = this
retroComponent = DaggerRetroComponent.builder().retroModule(RetroModule(APIURL.BASE_URL)).build()
//retroComponent.inject()

}



fun fetchRetroComponent():RetroComponent{
return retroComponent
}

override fun activityInjector(): AndroidInjector<Activity> {
return dispatchingAndroidInjector
}
}


我的 fragment 类如下:
我在 fragment 的 onAttach() 方法中调用了 dagger 相关代码:

RetroDIFragment:

      class RetroDIFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
lateinit var retroDIListViewModel: RetroDIListViewModel
lateinit var retroFitDIView: View
@Inject
lateinit var apiService: APIService

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
retroDIListViewModel = ViewModelProviders.of(activity!!).get(RetroDIListViewModel::class.java)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
retroFitDIView = inflater.inflate(R.layout.fragment_retro_di, container, false)

return retroFitDIView
}

override fun onAttach(context: Context?) {
super.onAttach(context)
AndroidInjection.inject(activity)
KotlinTemplateApplication.instance.fetchRetroComponent().inject(this@RetroDIFragment)

retroDIListViewModel.fetchPostsFromWebSevice(apiService).observe(this,object : Observer<List<RetroModel>>{
override fun onChanged(t: List<RetroModel>?) {
for (i in t!!.indices)
println(t[i].id)
}

})
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment RetroDIFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
RetroDIFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}


我的组件如下:

逆向组件:

 @Singleton
@Component(modules = arrayOf(RetroModule::class))
interface RetroComponent {
fun inject(retroDIFragment: RetroDIFragment)
}


我的模块如下:

 @Module
public class RetroModule(var urlPath:String) {


init{
this.urlPath = urlPath
}

@Singleton
@Provides
fun provideServiceAPI(retrofit: Retrofit):APIService{
return retrofit.create(APIService::class.java)
}

@Singleton
@Provides
fun provideRetrofit():Retrofit{
val retrofit = Retrofit.Builder()
.baseUrl(urlPath)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(providesOkHttpClientBuilder())
.build()
return retrofit

}


private fun providesOkHttpClientBuilder(): OkHttpClient {

val httpClient = OkHttpClient.Builder()
return httpClient.readTimeout(1200, TimeUnit.SECONDS)
.connectTimeout(1200, TimeUnit.SECONDS).build()

}
}


我的 Activity 如下

class RetroFitActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_retro_fit)
supportFragmentManager.beginTransaction().replace(R.id.container_retro_di, RetroDIFragment()).commit()
}
}


我在我的 Gradle 中包含了以下代码:

 implementation 'com.google.dagger:dagger:2.19'
implementation 'com.google.dagger:dagger-android:2.19'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.19'
annotationProcessor 'com.google.dagger:dagger-compiler:2.19'
kapt 'com.google.dagger:dagger-android-processor:2.19'
kapt 'com.google.dagger:dagger-compiler:2.19'
//moxy
compile 'com.arello-mobile:moxy-app-compat:1.1.1'
kapt 'com.arello-mobile:moxy-compiler:1.1.1'




谁能帮我解决这个问题。

最佳答案

您需要更改 AppComponent 类的注入(inject)方法参数:

@Singleton
@Component(modules = [
AndroidInjectionModule::class,
ActivityModule::class,
AppModule::class
])
interface AppComponent {

@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder

fun build(): AppComponent
}

fun inject(app: Application) // This is the piece of code you need to change
}
@Singleton
@Component(modules = [
AndroidInjectionModule::class,
ActivityModule::class,
AppModule::class
])
interface AppComponent {

@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder

fun build(): AppComponent
}

fun inject(app: YourCustomAppClass) // Change to your custom app class
}

还有,你在哪里做的

  DaggerAppComponent
.builder()
.application(yourAppInstance)
.build()
.inject(yourAppInstance)

您的 AppInstance 需要属于 YourCustomApp 类而不是 Application。

关于android - lateinit property dispatchingAndroidInjector 还没有初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195678/

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