我目前在嵌套片段时遇到问题,我创建了一个带有两个选项卡的SlidingTabLayout
,每个选项卡都有相应的片段,在一个选项卡中,我向FragmentPagerAdapter
返回的父片段添加了5个子片段,但是我在父母和孩子之间的沟通上有困难,
@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)
我只想知道子片段如何从父片段接收数据。
最佳答案
问题是,在实际添加片段之前,您正试图通过其标记来找到它。注意documentation表示FragmentTransaction.commit()
:
安排此事务的提交。不会发生提交
立即;它将被安排为
在下一次线程准备好时完成。
正如您所看到的,片段没有立即添加,所以这就是当您试图获取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
。您可以找到创建参数here的Fragment
的示例。更新
正如在冗长的评论中所指出的,尽管第一种方法允许
getChildFragmentManager().findFragmentByTag("year08")
返回片段,但在调用Fragment
之前或之后调用onViewStateRestored(...)
上访问其视图的任何方法仍然不是一个好主意。这是因为当使用
FragmentManager
添加父片段时,调用onCreateView
后不会立即调用子片段的executePendingTransactions()
方法,因此子片段的引用视图为空。如果父片段Fragment
直接在布局中使用,则这不是问题,因为子片段的onCreateView
被调用为executePendingTransactions()
的一部分,其视图将不为空。在这种情况下,最好的做法是使用前面提到的另一种方法,如果需要为片段中的视图设置初始值,则使用
Fragment
参数。