简体   繁体   English

使用 ViewBinding 共享 Lifecycle Livedata ViewModel

[英]Shares of Lifecycle Livedata ViewModel with ViewBinding

I'm still new to Kotlin.我还是 Kotlin 的新手。
So I want to try Lifecycle, LiveData, and ViewModel in my study project, using moviedb as its data.所以我想在我的研究项目中尝试Lifecycle、LiveData和ViewModel,使用moviedb作为它的数据。 But since Android Kotlin Extensions dependency is deprecated, I have to use viewBinding as an alternative, but when I run the app, it goes error like this:但是由于不推荐使用 Android Kotlin Extensions 依赖项,因此我必须使用 viewBinding 作为替代方案,但是当我运行该应用程序时,它会出现如下错误:

2022-06-28 11:39:19.935 25250-25250/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: id.kotlin.belajar, PID: 25250
    java.lang.RuntimeException: Unable to start activity ComponentInfo{id.kotlin.belajar/id.kotlin.belajar.presentation.HomeActivity}: java.lang.IllegalStateException: Base URL required.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3686)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3823)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7892)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
     Caused by: java.lang.IllegalStateException: Base URL required.
        at retrofit2.Retrofit$Builder.build(Retrofit.java:623)
        at id.kotlin.belajar.di.module.HomeModule$Companion.provideRetrofit(HomeModule.kt:25)
        at id.kotlin.belajar.di.module.HomeModule_Companion_ProvideRetrofitFactory.provideRetrofit(HomeModule_Companion_ProvideRetrofitFactory.java:29)
        at id.kotlin.belajar.di.module.HomeModule_Companion_ProvideRetrofitFactory.get(HomeModule_Companion_ProvideRetrofitFactory.java:21)
        at id.kotlin.belajar.di.module.HomeModule_Companion_ProvideRetrofitFactory.get(HomeModule_Companion_ProvideRetrofitFactory.java:11)
        at id.kotlin.belajar.di.module.HomeModule_ProvidesHomeDatasourceFactory.get(HomeModule_ProvidesHomeDatasourceFactory.java:29)
        at id.kotlin.belajar.di.module.HomeModule_ProvidesHomeDatasourceFactory.get(HomeModule_ProvidesHomeDatasourceFactory.java:13)
        at id.kotlin.belajar.presentation.HomeViewModel_Factory.get(HomeViewModel_Factory.java:27)
        at id.kotlin.belajar.presentation.HomeViewModel_Factory.get(HomeViewModel_Factory.java:11)
        at id.kotlin.belajar.di.factory.ViewModelFactory.create(ViewModelFactory.kt:13)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at id.kotlin.belajar.presentation.HomeActivity$viewModel$2.invoke(HomeActivity.kt:26)
        at id.kotlin.belajar.presentation.HomeActivity$viewModel$2.invoke(HomeActivity.kt:24)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at id.kotlin.belajar.presentation.HomeActivity.getViewModel(HomeActivity.kt:24)
        at id.kotlin.belajar.presentation.HomeActivity.onCreate(HomeActivity.kt:38)
        at android.app.Activity.performCreate(Activity.java:8285)
        at android.app.Activity.performCreate(Activity.java:8264)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)

So I want to know how to the said APIs using ViewBinding.所以我想知道如何使用 ViewBinding 来使用上述 API。
I need some shares or guides to do it.我需要一些分享或指南来做到这一点。 Thank you.谢谢你。

Link: https://github.com/muhammadwibisonojanuar/Coding-2.git链接: https ://github.com/muhammadwibisonojanuar/Coding-2.git

NetworkModule for Base_URL: Base_URL 的网络模块:

import dagger.Module
import dagger.Provides
import hu.akarnokd.rxjava3.retrofit.RxJava3CallAdapterFactory
import id.kotlin.belajar.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
class Networkmodule {

    @Provides
    @Singleton
    fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor{
        return HttpLoggingInterceptor().apply {
            level = when (BuildConfig.DEBUG){
                true -> HttpLoggingInterceptor.Level.BODY
                false -> HttpLoggingInterceptor.Level.NONE
            }
        }
    }

    @Provides
    @Singleton
    fun providesHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
        return OkHttpClient.Builder().apply {
            retryOnConnectionFailure(true)
            addInterceptor(interceptor)
        }.build()
    }

    @Provides
    @Singleton
    fun providesHttpAdapter(client: OkHttpClient): Retrofit.Builder {
        return Retrofit.Builder().apply {
            client(client)
            baseUrl(BuildConfig.BASE_URL)
            addConverterFactory(GsonConverterFactory.create())
            addCallAdapterFactory(RxJava3CallAdapterFactory.createAsync())
        }
    }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM