-1

Ich habe noch nie ein Fragment gemacht und habe versucht, dies zum Laufen zu bringen. Ich habe einige Online-Tutorials verfolgt, aber diese haben nicht viel gebracht.Wie mache ich ein Fragment?

Mein Problem ist, dass die Methoden aufgerufen werden außer Betrieb. Wenn die XML-Datei von MyActivity geladen wird, wird auch das Fragment geladen und die onCreate() -Methode aufgerufen. Dies geschieht, bevor ich die Methode newInstance() des Fragments aufgerufen habe, damit onCreate() nicht über die Parameter verfügt, die es benötigt, um zu funktionieren.

Wie können Parameter an das Fragment übergeben werden? Weil das offensichtlich nicht funktioniert. Ich habe versucht, den Code zu vereinfachen, was genau relevant ist.

Incase wollen Sie es, das die spezifische Fehler Ich erhalte:

android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment 

Linie # 18 Punkte auf die erste Zeile des Fragments Tag.

MyFragment.java

public class MyFragment extends Fragment { 
     private static final String ARG_PARAM1 = "param"; 
     private int param 

     public static MyFragment newInstance(int param) { 
      MyFragment fragment = new MyFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_PARAM1, param); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      this.param = getArguments().getInt(ARG_PARAM1); 
      // do stuff with param, but obviously param is not set 
      // since constructor is called. 
     } 

     public interface OnFragmentInteractionListener { 
      void onFragmentInteraction(String username); 
     } 

} 

MyActivity.java

public class MyActivity extends AppCompatActivity implements MyFragment.OnFragmentInteractionListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_follower_list); // crashes at this line 

     Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING, "TestingUsername"); 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     transaction.replace(R.id.my_fragment, newFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 

    @Override 
    public void onFragmentInteraction(String username) { 
     // TODO (I'm just trying to get it to run first.... 
    } 
} 

activity_my.xml

<RelativeLayout 
    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=".activities.MyActivity"> 

    <!-- The following fragment's onCreate() method is called before newInstance() is called, causing issues. --> 
    <fragment 
     class ="xyz.mydomain.myproject.fragments.MyFragment" 
     android:id="@+id/my_fragment" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="10dp"/> 

</RelativeLayout> 
+0

'newInstance' sein ist IHR Factory-Methode und es wird nie aufgerufen, wenn Sie das Fragment setzen direkt in der Tätigkeit Layout. –

Antwort

1

uns e getSupportFragmentManager() anstelle von getFragmentManager():

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_follower_list); // crashes at this line 

    Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING, "TestingUsername"); 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.fragment_container, newFragment); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
} 

Auch in Ihrem Fragmente Klasse sicher sein, es aus dem Android Framework Unterstützung Fragmente erbt:

import android.support.v4.app.Fragment; 

public class MyFragment extents Fragment { 
    private static final String ARG_PARAM1 = "param"; 
    private int param 

    public static MyFragment newInstance(int param) { 
     MyFragment fragment = new MyFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_PARAM1, param); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.param = getArguments().getInt(ARG_PARAM1); 
     // do stuff with param, but obviously param is not set 
     // since constructor is called. 
    } 

    public interface OnFragmentInteractionListener { 
     void onFragmentInteraction(String username); 
    } 

} 

Auch dieses Layout verwenden:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".activities.MyActivity"> 
</RelativeLayout> 
+0

Immer noch der gleiche Fehler. Es muss einen weiteren Fehler geben, da diese Codezeile nicht einmal ausgeführt wird, es stürzt zwei Zeilen zuvor auf der setContentView ab (stuff) ;. – CaptainForge

+0

Und ja, MyFragment erweiterte Fragment. Entschuldigung, diesen Teil versehentlich aus dem Code entfernt. – CaptainForge

+0

ok, ich habe gerade meine Antwort bearbeitet, versuche das Layout zu verwenden, ohne das Fragment darin zu legen ... stelle sicher, dass die ID des Wurzel-Layout-Elements mit der ID identisch ist, die du in der Fragment-Transaktion benutzt hast –

0

Erstellen Sie XML in Res-Ordner

<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.androiddeveloper.demoapp.MyFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

Java-Code hier.

import android.support.v4.app.Fragment; 

public class MyFragment extents Fragment { 
    private static final String ARG_PARAM1 = "param"; 
    private int param 

    public static MyFragment newInstance(int param) { 
     MyFragment fragment = new MyFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_PARAM1, param); 
     fragment.setArguments(args); 
     return fragment; 
    } 
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_my, container, false); 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.param = getArguments().getInt(ARG_PARAM1); 
     // do stuff with param, but obviously param is not set 
     // since constructor is called. 
    } 

    public interface OnFragmentInteractionListener { 
     void onFragmentInteraction(String username); 
    } 

} 

xml auf Ihrer Tätigkeit

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".activities.MyActivity"> 
</RelativeLayout> 

Aufruf in YourActivity

public class MyActivity extends AppCompatActivity implements MyFragment.OnFragmentInteractionListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_follower_list); // crashes at this line 

     Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING, "TestingUsername"); 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     transaction.replace(R.id.my_fragment, newFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 

    @Override 
    public void onFragmentInteraction(String username) { 
     // TODO (I'm just trying to get it to run first.... 
    } 
} 
0

könnte das Semikolon sein, haben Sie nicht Semikolon hinzugefügt.

statt private int param sollte private int param;

0

Verwenden

getSupportFragmentManager().beginTransaction(); 

statt

getFragmentManager().beginTransaction();