嵌套碎片通信

时间:2016-02-09 19:49:08

标签: android android-fragments android-nested-fragment

我目前在嵌套片段方面遇到了麻烦,我创建了一个带有两个标签的SlidingTabLayout,每个标签都有相应的片段,在一个标签中我添加了5个子片段给返回的父片段FragmentPagerAdapter,但我在父片段和它的片段子节点之间的通信方面遇到了麻烦,

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

            FragmentTransaction childManager=getChildFragmentManager().beginTransaction();
            childManager.add(R.id.linear08,new YearFragment08(),"year08");
            childManager.commit();
            YearFragment08 year08=(YearFragment08)getChildFragmentManager().findFragmentByTag("year08");
            year08.setinfo("This is fantastic");


}

`

public class YearFragment08 extends Fragment {
TextView yearInfo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.year_frag08,container,false);
    yearInfo=(TextView)v.findViewById(R.id.txt_yearInfo_08);
    return v;
}
 public void setinfo(String data){
    yearInfo.setText(data);
}

这就是结果。

02-09 21:45:48.078    2177-2177/com.compassasu.compass E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.compassasu.compass, PID: 2177
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.compassasu.compass.Fragments.YearFragment08.setinfo(java.lang.String)' on a null object reference
        at com.compassasu.compass.Fragments.HistoryFragment.onActivityCreated(HistoryFragment.java:33)
        at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1970)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我想知道的是子片段如何从父片段接收数据。

1 个答案:

答案 0 :(得分:1)

问题是您在实际添加之前尝试通过其标记找到该片段。请注意FragmentTransaction.commit()的{​​{3}}:

  

计划此交易的提交。提交不会发生   立即;它将被安排为主线程上的工作   下次线程准备就绪时完成。

正如您所看到的那样,片段未立即添加,因此当您尝试获取null时会返回Fragment

要解决此问题,您可以在提交事务后使用FragmentManager.executePendingTransactions()立即添加片段:

getChildFragmentManager().beginTransaction()
        .add(R.id.linear08,new YearFragment08(),"year08")
        .commit();

getChildFragmentManager().executePendingTransactions();
YearFragment08 year08=(YearFragment08)getChildFragmentManager().findFragmentByTag("year08");
year08.setinfo("This is fantastic");

另请注意,更好的方法是先创建YearFragment08片段,然后使用Fragment参数传递要在Fragment中显示的信息。然后,您不需要使用上述解决方法,因为首次创建yearInfo.setText("Hi")时无需致电Fragment。您可以找到使用参数documentation创建Fragment的示例。

<强>更新

正如冗长的评论所述,尽管第一种方法允许getChildFragmentManager().findFragmentByTag("year08")返回片段,但在Fragment上调用访问其视图的任何方法仍然不是一个好主意,直​​到{{1}或之后被召唤。

这是因为当使用onViewStateRestored(...)添加父片段时,在调用FragmentManager之后不会立即调用子片段的onCreateView方法,因此子片段&#39 ; s引用的视图为null。如果在布局中直接使用父executePendingTransactions(),则这不是问题,因为子片段的Fragment被称为onCreateView的一部分,并且其视图不会为空。

在这种情况下,最好的办法是使用上面提到的另一种方法,如果需要为片段中的视图设置初始值,则使用executePendingTransactions()参数。