由java.lang.IllegalStateException引起:无法为分离的片段创建ViewModelProvider [英] Caused by java.lang.IllegalStateException: Can't create ViewModelProvider for detached fragment

查看:728
本文介绍了由java.lang.IllegalStateException引起:无法为分离的片段创建ViewModelProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来解决以下崩溃问题.我通过在viewpager中调用fragment刷新其列表来刷新Activity中的重新启动列表.以下是崩溃的堆栈跟踪:

I need some help to ressolve the following crash. I am refreshing my list on Restart in Activity by calling fragment in viewpager to refresh its list. Following is the stacktrace of the crash:

Caused by java.lang.IllegalStateException: Can't create ViewModelProvider for detached fragment
       at android.arch.lifecycle.ViewModelProviders.checkActivity(ViewModelProviders.java:51)
       at android.arch.lifecycle.ViewModelProviders.of(ViewModelProviders.java:105)
       at com.ui.fragments.mainpagerfragments.ConversationListFragment.searchConversation(ConversationListFragment.java:383)
       at com.ui.activities.HomeActivity.onRestart(HomeActivity.java:288)
       at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1256)
       at android.app.Activity.performRestart(Activity.java:6365)
       at android.app.Activity.performResume(Activity.java:6376)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3299)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3345)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loop(Looper.java:207)
       at android.app.ActivityThread.main(ActivityThread.java:5728)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

从活动onRestart调用:

call from activity onRestart :

((MyListFragment) adapter.getFragmentAtPosition(0)).search("");

片段中的方法:

public void search(String query) {

        query = "%" + query + "%";
        if (myListViewModel == null) {
            myListViewModel = ViewModelProviders.of(this, viewModelFactory).get(MyListViewModel.class);
        }
        if (myListViewModel.mList != null && myListViewModel.mList.hasActiveObservers()) {
            myListViewModel.mList.removeObservers(this);
        }
        myListViewModel.getFilteredList(query).observe(this, mainObserver);
    }

推荐答案

如果您使用的是Viewpager,请确保相应的片段实际上已附加到其活动中(这就是该错误试图告诉您的内容).可能会出现该片段不可见,因此未附加该片段的情况(取决于视图分页器的配置方式).

If you're using a viewpager, be sure that the corresponding fragment is actually attached to its activity (this is what the error tries to tell you). It might happen that the fragment is not visible, thus it's not attached (depending on how the view pager was configured).

快速而肮脏的解决方法:

a quick&dirty fix:

public void search(String query) {

    if(!isAdded()) return; //<---- returns if the fragment is not attached

    query = "%" + query + "%";
    if (myListViewModel == null) {
        myListViewModel = ViewModelProviders.of(this, viewModelFactory).get(MyListViewModel.class);
    }
    if (myListViewModel.mList != null && myListViewModel.mList.hasActiveObservers()) {
        myListViewModel.mList.removeObservers(this);
    }
    myListViewModel.getFilteredList(query).observe(this, mainObserver);
}

但是,您应该对体系结构进行重新研究,因为这些检查通常暗示一些其他代码的味道.当您使用新的Android体系结构模式时,由于生命周期模式将为您处理所有这一切,因此无需进行此检查:

However, you should re-investigate in your architecture, because those checks often imply some other code smells. When you're working with the new Android Architecture Patterns, there should be no need of this check as the lifecycle pattern handles all this for you: https://developer.android.com/topic/libraries/architecture/lifecycle

因此,基本上,您不应直接直接调用片段的任何功能,而应调用相应的业务逻辑,该逻辑会通知片段以更新其视图.

So, basically, you should not directly call any function of a fragment directly, instead call the corresponding business logic, which notifies the fragment to update its views.

这篇关于由java.lang.IllegalStateException引起:无法为分离的片段创建ViewModelProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆