2016-11-20 6 views
-1

Ich versuche, der Aktivität Fragmente hinzuzufügen, durch Code, um zwischen ihnen auszutauschen, aber ich bekomme immer 'verursacht durch: android.view. InflateException: Binary XML-Datei Zeile # 13: Fehler aufblähenden Klasse Fragment‘verursacht durch: android.view.InflateException: Binäre XML-Datei Zeile # 13: Fehler beim Aufblasen des Klassenfragments

Hier ist der Code für die Haupttätigkeit:

package com.example.user.timetable_test; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 


public class Set_Up extends FragmentActivity{ 

    //MiscData data = MiscData.getInstance(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState){ 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_set__up); 

     FragmentManager fm = getSupportFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 

     Fragment wel = new frag_welcome(); 

     wel.setArguments(getIntent().getExtras()); 

     // Add the fragment to the 'fragment_container' FrameLayout 
     ft.add(R.id.fragment_container, wel); 


    } 


} 

XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_set_up" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.user.timetable_test.Set_Up"> 
    <fragment 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 
</RelativeLayout> 

für die fragme nt

package com.example.user.timetable_test; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class frag_welcome extends Fragment{ 

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

     View view = inflater.inflate(R.layout.frag_set_up_welcome, container, false); 

     return view; 
    } 
} 

XML-Datei:

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

    <TextView 
     android:text="@string/welcome_to_the_timetable_app" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" android:id="@+id/textView2" 
     android:layout_marginTop="100dp" 
     android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
     android:textAlignment="center" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"/> 
    <Button 
     android:text="@string/start_set_up_butt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="69dp" 
     android:id="@+id/startSetUp" style="@style/Widget.AppCompat.Button.Borderless" 
     android:layout_below="@+id/textView3" android:layout_centerHorizontal="true"/> 
    <TextView 
     android:text="@string/press_start_set_up_to_start" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView3" 
     android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
     android:textAlignment="center" 
     android:layout_marginTop="24dp" 
     android:layout_width="170dp" android:layout_below="@+id/textView2" 
     android:layout_centerHorizontal="true"/> 
</RelativeLayout> 
+1

Voll logcat, bitte –

Antwort

2

Lesen Sie den Code ...

// Fügen Sie das Fragment auf das 'fragment_container' FrameLayout

Sie wahrscheinlich brauchen zu ändern

Um

<FrameLayout 
    android:id="@+id/fragment_container" 

Oder nicht den Java-Code verwenden, um das Fragment zu erstellen und einfach das Klassenattribut zu dem Block XML-Fragments hinzuzufügen. Aus der Dokumentation ...

Das android:name Attribut im <fragment> gibt die Fragment-Klasse im Layout zu instanziieren.

Andernfalls können Sie das Fragment wie zeigen so

Fragment wel = new WelcomeFragment(); // correct naming schema 
wel.setArguments(getIntent().getExtras()); 
getSupportFragmentManager() 
    .beginTransaction() 
    .add(R.id.fragment_container, wel) 
    .commit(); 
+0

1. Ich wan später die Fragmente zu tauschen, ich lese, dass ich es durch das Java hinzufügen muss. 2. Jetzt bekomme ich keine Ausnahme, aber ich sehe auch nichts auf dem Bildschirm, nur eine leere Aktivität. – Omar

+0

Es ist das eine oder andere. Mit '' ist kein Java notwendig, um das Fragment anzuzeigen. Sie würden ein FrameLayout (oder eine andere ViewGroup) verwenden, wenn Sie die volle Kontrolle über die FragmentTransaction haben möchten. Sie müssen die FragmentTransaction nach der Add-Methode –

+0

begehen Ich bin ein Idiot, aber danke -_- – Omar

0

Erklären Sie Ihre fragment_container wie diese,

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_set_up" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.user.timetable_test.Set_Up"> 

    <!-- Can be any child of ViewGroup: RelativeLayout/LinearLayout/... --> 
    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 
</RelativeLayout> 
+0

Danke, jetzt bekomme ich keine Ausnahme, aber auch sehe ich nichts auf dem Bildschirm, nur eine leere Aktivität – Omar

+0

Sie müssen rufe 'ft.commit();' am Ende von onCreate auf –

Verwandte Themen