2017-06-01 3 views
-1

Je viens de commencer avec fragment android et git coincé lors de la mise en œuvre d'un exemple à partir d'internet.Attaching le code suivant ci-dessous.Il serait vraiment heureux d'obtenir de l'aide. Je reçois une erreur sur la ligne ft.add (R.id.fragmentone, fragment). Cordialement.Erreur dans la transaction Fragment

MainActivity.java

import android.app.Fragment; 
    import android.app.FragmentManager; 
    import android.app.FragmentTransaction; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    public class MainActivity extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     } 
     public void getfragment(View view) { 
      FragmentOne fragment = new FragmentOne(); 
      FragmentManager fm=getFragmentManager(); 
      FragmentTransaction ft=fm.beginTransaction(); 
      ft.add(R.id.fragmentone,fragment); 
     } 
    } 

FragmentOne.java

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
public class FragmentOne extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_fragment_one, container,false); 
    } 
} 

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.vighnesh.fragmentsexp.MainActivity" 
    android:orientation="vertical"> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/buttonfragment" 
    android:layout_margin="20dp" 
    android:text="Click to open fragment"/> 
    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/fragmentone" 
     android:layout_margin="20dp"> 
    </fragment> 
</LinearLayout> 

fragment_fragment_one.xml

<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="com.example.vighnesh.fragmentsexp.FragmentOne"> 
    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 
</FrameLayout> 
+0

quel problème ??? –

+0

Je pense que vous avez oublié d'appeler la méthode getfragment() de la méthode onCreate de votre activité, vous devez également valider votre transaction de fragment par ft.commit(); –

+0

Pas un bon moyen 'retour inflater.inflate (R.layout.' –

Répondre

0

Remplacez ce code dans votre activité.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getfragment(); 

} 

public void getfragment() { 
    FragmentOne fragment = new FragmentOne(); 
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.add(R.id.fragmentone, fragment); 
    ft.commit(); 
} 
0

changez-le.

import android.app.Fragment; 
    import android.app.FragmentManager; 
    import android.app.FragmentTransaction; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    public class MainActivity extends AppCompatActivity { 

     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btnFragOne = (Button)findViewById(R.id.buttonfragment); 
     btnFragOne.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       FragmentOne fragment = new FragmentOne(); 
       FragmentManager fm=getFragmentManager(); 
       FragmentTransaction ft=fm.beginTransaction(); 
       ft.add(R.id.fragmentone,fragment); getfragment(); 
      } 
     }); 

    } 
    } 
0

donc utiliser getSupportFragmentManager() au lieu de getFragmentManager()

Parce que le FragmentOne est de la bibliothèque de support (android.support.v4.app.Fragment) pas du cadre (android.app.Fragment), vous devez utiliser android.support.v4.app.FragmentManager pour faire transaction avec FragmentOne.