简体   繁体   English

ViewModelProviders.of(activity) 只接受片段或片段活动

[英]ViewModelProviders.of(activity) only accept fragment or fragmentactivity

i have a problem using mvvm in splash activity and relate the viewmodel to the activity,i don't won't to create fragment for it .我在启动活动中使用 mvvm 并将视图模型与活动相关联时遇到问题,我不会为它创建片段。 in AppCompatActivity:在 AppCompatActivity 中:

fun <T : ViewModel> AppCompatActivity.obtainViewModel(viewModelClass: Class<T>) =
    ViewModelProviders.of(this, ViewModelFactory.getInstance(application)).get(viewModelClass)

it says that .of(this,factor) only accept fragment and fragmentActivity i already read link but it doesn't solve the problem my build.grable:它说 .of(this,factor) 只接受 fragment 和 fragmentActivity 我已经阅读了链接,但它没有解决我的 build.grable 问题:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.attendance.myproject.begory"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // to prevent the addition of jvm-target
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildFeatures {
        dataBinding true
    }
}

dependencies {
    def room_version = "2.2.3" // check latest version from docs

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10"
    implementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion"
    implementation "androidx.cardview:cardview:$rootProject.cardViewVersion"
    implementation "androidx.constraintlayout:constraintlayout:2.0.2"
    //noinspection GradleCompatible

    implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
//room

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    implementation "com.google.android.material:material:1.3.0-alpha03"
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.1'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.1'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:17.5.1'
    implementation 'com.google.firebase:firebase-auth:19.4.0'
    implementation 'com.google.firebase:firebase-database:19.5.1'
    implementation 'com.firebase:firebase-client-android:2.3.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'me.dm7.barcodescanner:zxing:1.9.8'

    //    Add Zxing library to generate barcode
    implementation 'com.google.zxing:core:3.2.1'
    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.4.10"
    //curved bottom navigation
    implementation 'np.com.susanthapa.curved_bottom_navigation:curved_bottom_navigation:0.6.3'

    implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion"
    kapt "androidx.lifecycle:lifecycle-compiler:$archLifecycleVersion"
    implementation "androidx.core:core-ktx:1.3.2"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
    implementation'android.arch.lifecycle:extensions:1.1.1'
}
apply plugin: 'com.google.gms.google-services'

I'm using androidx and the activity is extended from the appcompatActivitty .I had initiate the viewmodel in the on create fun .我正在使用 androidx 并且活动是从 appcompatActivitty 扩展的。我在 on create fun 中启动了视图模型。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_splash)
    if(!Internet.isOnline(applicationContext)) showMessage(getString(R.string.no_internet_connection))

    binding= DataBindingUtil.setContentView(this, R.layout.activity_splash)
    splashViewModel = obtainViewModel()
    binding.splashViewModel
    subscribeToNavigationChanges(splashViewModel)

}

I really takes days searching in this problem if any one can help it will be very thankful.我真的需要几天时间来搜索这个问题,如果有人可以提供帮助,我将非常感激。

The problem is that you're using the wrong ViewModelProviders import.问题是您使用了错误的ViewModelProviders导入。 You need to remove this line from your dependencies:您需要从依赖项中删除此行:

implementation 'android.arch.lifecycle:extensions:1.1.1'

android.arch.lifecycle is the old, pre-AndroidX artifacts that you should not and cannot use when using AndroidX dependencies. android.arch.lifecycle是旧的、pre-AndroidX 工件,在使用 AndroidX 依赖项时不应该也不能使用。 You already have the androidx.lifecycle:lifecycle-extensions dependency, which is the full replacement for android.arch.lifecycle:extensions , which actually has the right version of ViewModelProviders that works with the AndroidX version of AppCompatActivity .您已经拥有androidx.lifecycle:lifecycle-extensions依赖项,它是android.arch.lifecycle:extensions的完全替代品,它实际上具有与AppCompatActivity的 AndroidX 版本配合使用的正确版本的ViewModelProviders

Note that there's no reason to write or use any method like your obtainViewModel since this is already provided for you by val viewModel: YourViewModel by viewModels() that is part of the activity-ktx dependency (that you already get as a transitive dependency through navigation-fragment-ktx .请注意,没有理由编写或使用任何方法,例如您的obtainViewModel因为这已经由val viewModel: YourViewModel by viewModels()为您提供val viewModel: YourViewModel by viewModels()您的val viewModel: YourViewModel by viewModels() ,它是activity-ktx依赖项的一部分(您已经通过navigation-fragment-ktx作为传递依赖项获得) navigation-fragment-ktx

暂无
暂无

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

相关问题 Kotlin 不理解 ViewModelProviders.of(activity ?: fragment) - Kotlin does not understand ViewModelProviders.of(activity ?: fragment) ViewModelProviders.of(FragmentActivity).get(ViewModel :: class.java)要求活动而不是get()调用中的viewmodel - ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java) asking for activity instead of viewmodel in get() call 如何在Non-FragmentActivity上使用ViewModelProviders.of()获取ViewModel? - How to fetch the ViewModel using ViewModelProviders.of() on non-FragmentActivity? 错误:找不到方法ViewModelProviders.of(Fragment,Factory)的合适方法 - error: no suitable method found for method ViewModelProviders.of(Fragment,Factory) ViewModelProviders.of(getActivity()) “无法解析(android.app.Activity)的方法” - ViewModelProviders.of(getActivity()) “Cannot resolve method of(android.app.Activity)” 当 ViewModelProviders.of() 被弃用时,如何在 Activity 和 Service 之间共享相同的 ViewModel? - How to share the same ViewModel between Activity and Service when ViewModelProviders.of() is deprecated? FragmentActivity TabHost包含片段和活动 - FragmentActivity TabHost with Fragment and Activity FragmentActivity:为未知片段提供的活动结果 - FragmentActivity﹕ Activity result delivered for unknown Fragment 直接实例化ViewModels,而不使用ViewModelProviders.of方法 - Instantiate ViewModels directly, without making use of ViewModelProviders.of method 由于不推荐使用 ViewModelProviders.of(),我应该如何创建 ViewModel 的 object? - As ViewModelProviders.of() is deprecated, how should I create object of ViewModel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM