2013-02-20 15 views
16

ich eine Fragment haben, die eine TabHost als Root-Layout hat wie folgt ...Fragment getArguments() gibt null

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <FrameLayout 
       android:id="@+id/tab_1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 

      <!-- More FrameLayouts here - each are placeholders for Fragments -->  

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

Der Code erstellen/aktualisieren jeden Fragment für die Registerkarte Inhalt ist wie folgt ..

.
private void updateTab(String tabId, int placeholder) { 
    FragmentManager fm = getFragmentManager(); 
    if (fm.findFragmentByTag(tabId) == null) { 
     Bundle arguments = new Bundle(); 
     arguments.putInt("current_day", mCurrentTab); 
     EpgEventListFragment fragment = new EpgEventListFragment(); 
     fragment.setArguments(arguments); 

     fm.beginTransaction() 
       .replace(placeholder, new EpgEventListFragment(), tabId) 
       .commit(); 
    } 
} 

im onCreate(...) Methode des EpgEventListFragment ich dann versuchen, die Argumente zu bekommen Bundle aber ich bekomme immer null Sie folgendermaßen vorgehen ...

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

    Bundle arguments = getArguments(); 
    if (arguments == null) 
     Toast.makeText(getActivity(), "Arguments is NULL", Toast.LENGTH_LONG).show(); 
    else 
     mCurrentDay = getArguments().getInt("current_day", 0); 

    ... 
} 

Was fehlt mir hier? Ich habe auch versucht in onAttach(...), aber ich habe immer noch null. Ich bin neu in der Verwendung von Fragments, also hoffe ich, dass es einen einfachen Grund gibt, aber ich habe bei der Suche nichts gefunden.

Antwort

43

Ich denke, dies mit Ihrem Problem zu tun hat:

fm.beginTransaction() 
    .replace(placeholder, new EpgEventListFragment(), tabId) 
    .commit(); 

Sie eine neue Fragment machen (das nicht Argumente haben, da es frisch instanziiert ist schon).

Stattdessen versuchen

Fragment fragment = new EpgEventListFragment(); 
fragment.setArguments(arguments); 
fm.beginTransaction() 
    .replace(placeholder, fragment, tabId) 
    .commit(); 
+10

Oh du mich verarschen haben werden ... aaaargh! – Squonk

+2

Manchmal braucht es nur ein anderes Augenpaar. Vielen Dank! Ich habe ursprünglich keine Argumente übergeben, also habe ich das 'Fragment' im Aufruf von' replace (...) 'erstellt. Es gibt eine Zeitverzögerung für mich, Ihre Antwort zu akzeptieren, aber ich werde es tun. Danke noch einmal. – Squonk

+1

@Squonk du bist willkommen! Ich erinnere mich, dass ich einmal vergessen habe, "FragmentActivity" zu erweitern. Es war die beste halbe Stunde meines Lebens versuchen, das herauszufinden :-) –

Verwandte Themen