Layout fields of fragment are NULL on initialization

吃可爱长大的小学妹 提交于 2019-12-05 11:33:18

I'm pretty sure your issues are due to keeping the references to fragments in an Array. Fragments have lifecycles and the references are not guaranteed to persist. As you said, it's hard to reproduce and track down exactly what's going wrong, but maybe you don't need to. Some suggestions on how to fix this:

  1. Do not store the references to fragments. Pretty much follow the example on Google's page (https://developer.android.com/training/animation/screen-slide) and instantiate a new fragment every time it's requested.

  2. If you are worried about performance and caching is solving it, try using FragmentStatePagerAdapter - it caches pages and manages fragments' states.

  3. If you need to access page fragments from the main fragment (or the activity), instead of storing references, use `findFragmentByTag' which will always return the currently active instance of the fragment.

in my experience if there is an error in fragments it is usually because of pre-loading of fragments in viewpager and TabMenu so what I did and Suggest you do to is to check if the fragment is visible to user and if they were, get data and other things so here is my code:

public class Fragment1 extends Fragment {
boolean visible = false;
public static Fragment1 newInstance() {
    return new Fragment1();
}
@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    if (visible && isResumed()) {
        onResume();
    } else if (visible) {
        getMyData();
    }
    return rootView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    visible = isVisibleToUser;
    if (isVisibleToUser) {
        getMyData();
    }
    else {
    }
}

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

this way if the view is not created yet and is not visible to user fragment won't do anything .

hope this helps.

I strongly believe that things aren't that complicated as others explained. If i understand it correctly, the delay caused by the network transaction is the culprit.

Consider this scenario.

  • You are making a network request which makes some changes in the view at the end.
  • You switch the pager. (Fragment is detached, views are destroyed)
  • Here comes the callback from the network request. Guess what! A crash.

So when dealing dealing with views of fragments, it's always a good idea to be more careful. I usually do this.

//in the base class
protected boolean isSafe()
    {
        return !(this.isRemoving() || this.getActivity() == null || this.isDetached()
                || !this.isAdded() || this.getView() == null);
    }

//usage in derived classes
onNewtworkResult(Result result) {
    if(!isSafe())
        return;
    //rest of the code
}

Alternatively, You can wrap the potential code in a try catch too. But that is more like a blind shot (At least in this case).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!