2017-03-09 90 views
3

這是我的孩子片段,以及如何從這個片段訪問我的父母片段,請幫我解決它訪問父片段

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, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.enquiry_layout, container, false); 
return rootView;}} 
+0

我認爲EnquiryActivity是活動,所以你爲什麼使用片段過渡?請把EnquiryActivity的代碼 – Ibrahim

回答

9

你可以得到父母Fragment的參考作者:

ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment()); 
parentFrag.someMethod(); 

我在我的ViewPager中使用了這個。如果您有其他要求,InterfaceEventBus是兩個一般的要求。