-1

J'essaie de lancer un fragment à partir d'une activité. Cependant, quand je lance l'application et cliquez sur le bouton qui doit lancer le fragment je reçois l'erreur:Pourquoi la vue n'est-elle pas trouvée pour l'ID?

java.lang.IllegalArgumentException: No view found for id 0x7f0e0074 (com.example.hudhud.islam:id/kontaktfragment) for fragment Kontakt{aaaed67 #1 id=0x7f0e0074} 

Je ne vois pas où je l'ai fait quelque chose de mal. Cela devrait être correct. C'est la classe où j'implémente le fragment. Je suis seulement va télécharger le View onCreateView pour cette classe plus est nécessaire:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     final View view = inflater.inflate(R.layout.fragment_kontakt, container, false); 

     sendmail = (Button) view.findViewById(R.id.sendknap); 
     sendmail.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       msg = (EditText) view.findViewById(R.id.besked); 
       String message = msg.getText().toString(); 
       sendemail(message); 
      } 
     }); 

     return view; 
    } 

je lance le fragment d'ici:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if (item.getItemId() == R.id.Kontakt) { 
      Fragment fragment = new Kontakt(); 
      getFragmentManager().beginTransaction() 
        .add(R.id.kontaktfragment, fragment) 
        .commit(); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

Qu'ai-je manqué?

Toute aide est appréciée!

EDIT: C'est le XML pour l'activité:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#32c6a6" 
    android:weightSum="1"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:id="@+id/velkomst" 
     android:textSize="20dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="10dp" /> 

    <FrameLayout 
     android:id="@+id/Buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:layout_below="@id/velkomst" > 

    </FrameLayout> 

</RelativeLayout> 

Et c'est le XML kontakfrag:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="1"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/kontaktfragment"></FrameLayout> 
</RelativeLayout> 

Et c'est le XML fragment_kontakt:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#32c6a6" 
       android:weightSum="1"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/besked" 
     android:layout_weight="0.70" 
     android:textSize="20dp" 
     android:layout_marginTop="30dp" 
     android:textIsSelectable="true" 
     android:textColor="#000000" 
     android:background="#ffffff" 
     android:gravity="top" 
     android:paddingTop="30dp" 
     android:hint="Hvis du har feedback eller nogle spørgsmål, så er du velkommen til at skrive. Vi besvarer mailen hurtigst muligt" 
     android:scrollIndicators="right"/> 

    <Button 
     android:layout_width="144dp" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/sendknap" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="0.05" 
     android:textSize="20dp"/> 
</LinearLayout> 
+0

Le message 'Exception' vous dit qu'un' ViewGroup 'avec l'ID' kontaktfragment' ne peut pas être trouvé dans la disposition 'Activity'. –

+0

Pourquoi cela se produit-il? J'ai fait le fragment et l'ai appelé kontaktfragment – Hudhud

+0

Je ne sais pas ce que vous voulez dire, mais l'ID que vous passez comme premier argument dans cet appel 'add()' doit être l'ID d'un 'ViewGroup' dans le' Activity '. Vous n'avez pas fourni assez d'informations pour que nous puissions déterminer exactement ce que vous avez tort. –

Répondre

0

Mon erreur était que j'ai créé le fragment comme une mise en page séparée et non comme un framelayo ut sur le frontpage. Donc la solution était de créer un framelayout sur le frontpage. Cela a du sens :)

0

d'abord à la mise en page de l'activité, ajoutez cette

<FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

Alors dans ce code java activité ajouter

 Fragment fragment = new Kontakt(); 
     getFragmentManager().beginTransaction() 
       .replace(R.id.container, fragment) 
       .commit();