2016-05-13 29 views
2

Ich versuche, ein Fragment als Standardansicht für die Aktivität geladen zu haben, aber ich bekomme nur einen leeren Bildschirm und riesige Speicherlecks.Standardfragment auf Aktivität Starten

Aktivität Datei onCreate Methode:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_login); 

    if (savedInstanceState == null) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     int fragmentTransaction = fragmentManager.beginTransaction() 
       .add(R.id.login_screen_fragment, new FragmentLogin()) 
       .commit(); 
    } 
} 

Die XML für die Aktivität:

<FrameLayout 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" 
    tools:context=".LoginActivity"> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/login_screen_fragment" 
     class="com.test.project.FragmentLogin" 
    /> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/forgotten_password_fragment" 
     class="com.test.project.FragmentForgottenPassword" 
    /> 
</FrameLayout> 

und das Fragment Klasse (relevante Teile):

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    final View view = inflater.inflate(R.layout.fragment_login, container, false); 
    final Activity activity = getActivity(); 

    ... code ... 

    return view; 
} 

Ich folgte Anweisungen von einem Tutorial jemand schlug früher aus einer anderen Frage, aber ich muss Somethi tun furchtbar falsch. Irgendwelche Ideen?

+0

Wenn Sie definieren '' Elemente im Layout, brauchen Sie nicht diejenigen, dynamisch in Ihrem Code zu laden. –

+0

@MikeM. Wo mache ich das? – mwieczorek

+0

Der 'if'-Block instanziiert eine 'FragmentLogin'-Instanz dynamisch und verarbeitet sie. –

Antwort

0

Ersetzen Sie einfach das Fragment-Element mit FrameLayout, dann können Sie Ihr Fragment sehen.

0

Wenn Sie das Fragment programmatisch hinzufügen möchten, entfernen Sie die <Fragment> in Ihrem Framelayout und fügen Sie eine ID hinzu.

Es sollte wie folgt aussehen:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/login_screen_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".LoginActivity" /> 
2

XML-Datei

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/changeFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".LoginActivity"> 
</FrameLayout> 

Ihre Aktivität

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_login); 

if (savedInstanceState == null) { 
    FragmentLogin f1= new FragmentLogin(); 
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.add(R.id.changeFragment, f1); 
    fragmentTransaction.commit(); 

    } 
} 
1

Ich bin neu in Android und ich lief in die gleiches Problem. So habe ich meinen behoben.

Verwenden Sie also in Ihrer onCreate-Methode getSupportFragmentManager() aus der FragmentManager-Klasse und deklarieren Sie dann FragmentTransaction separat. Siehe unten. Ich habe das für meinen Fall getan und es hat funktioniert.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_login); 

if (savedInstanceState == null) { 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.login_screen_fragment, new FragmentLogin()); 
    fragmentTransaction.commit(); 
} 

Hoffe, das hilft Ihr Problem zu lösen.

Mit freundlichen Grüßen, sylvery

Verwandte Themen