Fragment之间相互调用

问题描述:在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之间参数的传递

你可能感兴趣的:(Fragment,相互调用)