2016-09-05 4 views
-1

J'essayais de passer d'un Fragment au second Fragment.
Il montre l'erreur suivante:Fragment: java.lang.IllegalArgumentException: Aucune vue trouvée pour l'ID 0x7f0c006f

java.lang.IllegalArgumentException: No view found for id 0x7f0c006f (com.chinatown.wangjian.chinatownbang:id/viewpager) for fragment TwoFragment{10598077 #1 id=0x7f0c006f android:switcher:2131492975:1} 

Ceci est mon premier fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.activity_scrolling, container, false); 
     // Inflate the layout for this fragment 

     getActivity().setContentView(R.layout.activity_scrolling); 
     Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
     ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 



     return v; 

Mon second fragment comme celui-ci:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_two, container, false); 
    } 

Suis-je coder cette correctement?

+1

Jetez un coup d'oeil [ici] (http://stackoverflow.com/questions/39332955/no-view-found-for-id-for-shooksfragment/39333117#39333117) – Shaishav

Répondre

1

Dans votre code, il semble que vous appliquiez 2 mises en page.

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_scrolling, container, false); 
    //why setContentView? 
    //getActivity().setContentView(R.layout.activity_scrolling); 
    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 



    return v; 
+0

merci c'est correct . Je viens de copier le contenu de Oncreate oublié de commenter setContentView. Merci beaucoup. –

1

Tout d'abord, selon votre code, vous n'appelez pas le second fragment du premier.

Ajouter ce code dans votre premier fragment comme ce

FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); 
//CREATE OBJECT OF FRAGMENT WHERE YOU WANT TO GO 
fragment_2 frag2=new fragment_2(); 
fragmentTransaction.add(R.id.fragment_container,frag2); 
fragmentTransaction.commit(); 

est comment vous pouvez lancer fragment suivant d'un fragment et vous appelle également deux mise en page dans votre fragment, il serait mieux si vous utilisez code ci-dessus ainsi que l'utilisation seulement 1 mise en page pour Fragment