2017-03-07 3 views
1

habe ich versucht, durch Zugabe eines Fragments (AnotherFragment) zu einem Layout (sample_content_fragment) in mainactivity ein Fragment dynamisch laden .Aber es nicht funktioniert, wenn ich button1.The Code unten haben so den Fehler ausgeführt klicken ist nicht der Button.Fragment In Arbeit nicht

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button1: 
     AnotherFragment fragment = new AnotherFragment(); 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.sample_content_fragment, fragment); 
     transaction.commit(); 
     break; 
    } 

Und hier können Sie mainactivity Layout sehen:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <fragment 
    android:id="@+id/fregment_test" 
    android:name="com.example.henucmapus.TestFragment" 
    android:layout_width="match_parent" 
    android:layout_height="112dp" /> 

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

Hier ist das Fragment ich möchte laden. Hier

import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class AnotherFragment extends Fragment{ 
public View OnCreateView(LayoutInflater inflater,ViewGroup container, 
      Bundle savedInstanceState){ 
    View view =inflater.inflate(R.layout.anotherfragment,container, false); 

    return view; 
} 
} 

ist das Fragment-Layout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/t1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="11111111111" 
     /> 

</LinearLayout> 
+0

sowieso getan Sieht aus wie Sie versuchen, den Inhalt in der anfänglichen zu ersetzen Fragment anstelle des Fragments selbst. – RScottCarson

Antwort

1

OnCreateView bedeutet nichts in Android Begriffe (zumindest in Java, Xamarin hat es aktiviert).

Die korrekte Methode, die Sie wünschen, ist onCreateView. (Kleinbuchstaben on)

Wenn Sie Anmerkungen verwendet, Sie konnte das sehen.

Zum Beispiel.

public class AnotherFragment extends Fragment{ 
    @Override 
    public View onCreateView(LayoutInflater inflater,ViewGroup container, 
      Bundle savedInstanceState){ 
     View view = inflater.inflate(R.layout.anotherfragment,container, false); 

     return view; 
    } 
} 

Und wenn das nicht funktioniert, Sie scheinen keine R.id.button1 in Ihrem Haupt-Layout zu haben, so gibt es keine Fragment Transaktion

+0

schöner Fang ..... –