-1

Ich versuche, ein Fragment von einer Aktivität zu starten. Allerdings, wenn ich die App und klicken Sie auf die Schaltfläche ausführen, die das Fragment ich den Fehler zu starten hat:Warum wird die Ansicht für die ID nicht gefunden?

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

Ich kann nicht sehen, wo ich etwas falsch gemacht habe. Es sollte korrekt sein. Dies ist die Klasse, in der ich das Fragment implementiere. Ich werde nur die View onCreateView für diese Klasse hochladen, wie nicht mehr benötigt wird:

@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; 
    } 

ich starten Sie das Fragment von hier:

@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); 
    } 

Was habe ich verpasst?

Jede Hilfe ist willkommen!

EDIT: Dies ist die XML für die Aktivität:

<?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> 

Und das ist die kontakfrag XML:

<?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> 

Und das ist die fragment_kontakt XML:

<?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

Die 'Exception' Nachricht sagt Ihnen, dass eine' Viewgroup 'mit ID' kontaktfragment' kann nicht im Layout der 'Aktivität' gefunden werden. –

+0

Warum passiert das? Ich habe das Fragment gemacht und es Kontaktfragment genannt – Hudhud

+0

Ich weiß nicht, was Sie meinen, aber die ID, die Sie übergeben, als das erste Argument in diesem 'add()' Aufruf muss die ID für eine 'ViewGroup' in der Aktivität sein '. Sie haben uns nicht genügend Informationen zur Verfügung gestellt, um feststellen zu können, was genau Sie falsch gemacht haben. –

Antwort

0

Mein Fehler war, dass ich das Fragment als separates Layout und nicht als framelayo erstellt habe ut auf der frontpage. Die Lösung bestand also darin, ein Frame-Layout auf der frontpage zu erstellen. Das macht Sinn :)

0

Zuerst, um das Layout der Tätigkeit, um dieses dann in dieser Tätigkeit Java-Code

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

add

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