【问题标题】:Fragment not attached to Activity while loading adapter加载适配器时片段未附加到 Activity
【发布时间】:2020-09-07 15:37:23
【问题描述】:

我在第二次加载 Fragment 时遇到此错误。这很奇怪,因为在第一次加载片段时没有任何问题。这是错误:

java.lang.IllegalStateException: Fragment ShopFragment{8f5d575} (89cc4724-c435-49f4-ad05-4de4f1d18fc9)} not attached to an activity.
    at androidx.fragment.app.Fragment.requireActivity(Fragment.java:833)
    at com.example.fragmentapp.ui.fragments.ShopFragment$4.onChanged(ShopFragment.java:138)

仅当我第二次打开片段并且应用程序崩溃时才会发生这种情况。我从 MVVM 和 Room 开始,所以我不是这方面的专家,所以我有点迷失。 这是我的片段中的代码

public class ShopFragment extends Fragment {

private ShopViewModel shopViewModel;

private Spinner spDeptoShop;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_shop, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    shopViewModel = new ViewModelProvider(requireActivity()).get(ShopViewModel.class);


    spDeptoShop = (Spinner) view.findViewById(R.id.sp_deparmento_shop);


    init();

    subscribeObservers();

}

private void init() {
    shopViewModel.getDeptosLocal();
}


private void subscribeObservers() {
    shopViewModel.getDeptos().observe(requireActivity(), new Observer<Resource<List<Departamento>>>() {
        @Override
        public void onChanged(Resource<List<Departamento>> listResource) {
            if(listResource != null){
                Log.d(TAG, "onChanged: status: " + listResource.status);
                if(listResource.data != null){
                    switch (listResource.status){
                        case LOADING:{
                            break;
                        }
                        case ERROR:{
                            Log.e(TAG, "onChanged: cannot refresh the cache." );
                            Log.e(TAG, "onChanged: ERROR message: " + listResource.message );
                            Log.e(TAG, "onChanged: status: ERROR, #DEPARTAMENTO: " + listResource.data.size());
                            break;
                        }
                        case SUCCESS:{
                            Log.d(TAG, "onChanged: cache has been refreshed.");
                            Log.d(TAG, "onChanged: status: SUCCESS, #DEPARTAMENTO: " + listResource.data.size());
                            if(listResource.data != null && listResource.data.size()>0)
                            {
                                spDeptoShop.setAdapter( new ArrayAdapter<Departamento>(requireActivity(), R.layout.spinner_list_item, listResource.data));
                            }
                            else{

                            }
                            break;
                        }
                    }
                }
            }
        }
    });


}

@Override
public void onPause() {
    super.onPause();
    shopViewModel.cancelRequest();
}

}

错误在一行:

                        spDeptoShop.setAdapter( new ArrayAdapter<Departamento>(requireActivity(), R.layout.spinner_list_item, listResource.data));

【问题讨论】:

    标签: android android-fragments android-activity android-room


    【解决方案1】:

    您正在使用requireActivity() 调用observe() - 您的活动的生命周期。这意味着您将继续收到结果,直到您的 Activity 停止 - 即,它根本不依赖于您的 Fragment 的生命周期,这意味着即使在您的 Fragment 分离后它也会继续接收结果。你不应该在片段中使用requireActivity()observe()

    片段有一个特殊的生命周期,专门与创建视图的时间相关 - getViewLifecycleOwner()。这是您应该为您的observe 使用的内容:

    shopViewModel.getDeptos().observe(getViewLifecycleOwner(), new Observer<Resource<List<Departamento>>>() {
    

    【讨论】:

    • 非常感谢!我不知道这一点。现在完美运行。
    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2018-09-20
    • 2011-10-15
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多