温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Fragment之间相互调用

发布时间:2020-07-11 15:11:14 来源:网络 阅读:596 作者:truesea 栏目:开发技术

问题描述:在onCreateView在获取到programListFragment,然后在onActivityCreated中调用programListFragment.getView(),就在这里出现空指针异常。

解决方案:在onActivityCreated中新建AsyncTask在doInBackground中sleep一下,然后在onPostExecute中调用programListFragment.getView(),这样处理以后就没有再出现上述空指针异常。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.fragment_watchtv, container, false);
    } catch (InflateException e) {
        /* fragment_main is already there, just return view as it is */
    }
    channelListFragment = (TvChannelListFragment) getFragmentManager().findFragmentById(R.id.channel_list_fragment);
    commentListFragment = (TvCommentListFragment) getFragmentManager().findFragmentById(R.id.comment_list_fragment);
    programListFragment = (TvProgramListFragment) getFragmentManager().findFragmentById(R.id.program_list_fragment);
    return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInVoid...  params) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
                                                                                                                                                                                                      
        protected void onPostExecute(Void result) {
            programListFragment.getView().setOnTouchListener(programTouchListener);
        }
                                                                                                                                                                                                      
    }.execute();
}


要求:在ChannelListFragment中调用ProgramListFragment的实例。ChannelListFragment和ProgramListFragment是平级关系。

实现:在ProgramListFragment类中创建static类型变量sInstance,在onCreate方法中给sInstance赋值,然后在ChannelListFragment中调用sInstance。

ProgramListFragment中处理逻辑

public static TvProgramListFragment sInstance = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sInstance = this;
}
public void update(ExtraData extra, ChannelItemData channel) {
    ImageRender.render(context, channelIcon, extra.getChannelIconURL());
    ((TextView) getView().findViewById(R.id.channelName)).setText(extra.getChannelName());
    programAdapter = new TvProgramAdapter(getActivity(), data, extra);
    programListView.setAdapter(programAdapter);
    if(ChannelSharedPreferenceManager.getInstance().getPref(channel.getChannelId(), "").equals("")) {
        btnOrder.setImageResource(R.drawable.txt_button_order);
    } else {
        btnOrder.setImageResource(R.drawable.txt_button_ordered);
    }
    this.extra = extra;
    this.channel = channel;
    loadProgramData();
}

ChannelListFragment中调用:

TvProgramListFragment.sInstance.update(extra, item);

相关链接:两个Fragment之间参数的传递

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI