这是我的子片段以及如何从这个片段访问我的父片段请帮我修复它
public class Profilefragmant extends Fragment { public Profilefragment(){} @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.profile_layout,container,false); TextView VE = (TextView) rootView.findViewById(R.id.profile_view_enquiry); VE.setOnClickListener(new View.OnClickListener(){ public void onClick (View view){ Fragment fragment = null; fragment = new Enquiryfragment(); replaceFragment(fragment); } }); return rootView; } public void replaceFragment(Fragment someFragment) { android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.containerView,someFragment); transaction.addToBackStack(null); transaction.commit(); } }
运行项目时这是我的错误
java.lang.IllegalArgumentException: No view found for id 0x7f0c0099 (com.knrmarine.knr:id/containerView) for fragment EnquiryActivity{3754711 #2 id=0x7f0c0099}
这是我的Enquiryfragment代码:
public class Enquiryfragmant extends Fragment { public Enquiryfragment(){} @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.enquiry_layout,false); return rootView;}}
解决方法
您可以通过以下方式获取父Fragment的参考:
ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment()); parentFrag.someMethod();
我在我的ViewPager中使用过它.如果您有其他要求,Interface或EventBus是两个常规要求.