getActivity在某些根深蒂固的设备onActivityCreated片段返回null [英] getActivity return null in fragment onActivityCreated in some rooted device

查看:124
本文介绍了getActivity在某些根深蒂固的设备onActivityCreated片段返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的大部分设备,但一​​些根深蒂固设备上的应用程序做工精细。在片段的 onActivityCreated getActivity 保留返回null。我需要的上下文类下面设置的东西。

因此​​,任何人能帮助我吗?

更新

 公共类BaseProductFragment扩展片段{...公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    查看rootView = inflater.inflate(layout_id,集装箱,
            假);
    返回rootView;
}公共无效onActivityCreated(最终捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    ImageCacheParams cacheParams =新ImageCacheParams(getActivity()
            Utils.PRODUCTS_CACHE_DIR);
    ....
}


解决方案

getActivity 可能返回如果尤其是在像方位的变化,因为活性被破坏的配置变化中的 onActivityCreated ...从...所谓的移动初始化 onAttach ...

  @覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
    //初始化这里
}

onActivityCreated 是一个调用时父活动的的onCreate 叫...但是请记住,它可能是重建,一个配置变化中被摧毁

My app work fine on most device but some rooted device. In fragment onActivityCreated, getActivity keep return null. I need Context class to to set up thing below.

So anyone can help me?

Update

public class BaseProductFragment extends Fragment {

...

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(layout_id, container,
            false);
    return rootView;
}

public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ImageCacheParams cacheParams = new ImageCacheParams(getActivity(),
            Utils.PRODUCTS_CACHE_DIR);
    ....
}

解决方案

getActivity "might" return null if called from within onActivityCreated...especially during a configuration change like orientation change because the activity gets destroyed...move that initialization to onAttach...

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    //initialize here
}

onActivityCreated is a called when the parent activity's onCreate is called...but remember that it could be "recreated", destroyed during a config change

这篇关于getActivity在某些根深蒂固的设备onActivityCreated片段返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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