2012-10-25 6 views
7

Ich implementiere Code aus einem Beispiel (http://arvid-g.de/12/android-4-actionbar-with-tabs Beispiel) und versuchen, es in ActionBarSherlock zu konvertieren. HierVERY Einfache actionbarsherlock-Registerkarten mit Fragmenten: FragmentTransaction ist null in der onTabSelected() -Methode

ist TabActivity.java:

package com.pnet; 

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.Tab; 
import com.actionbarsherlock.app.SherlockActivity; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.widget.Toast; 

public class TabActivity extends SherlockActivity { 

    private static String TAG = "TabActivity"; 
    public static Context appContext; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tab_activity); 

     //ActionBar gets initiated 
     ActionBar actionbar = getSupportActionBar(); 
     //Tell the ActionBar we want to use Tabs. 
     actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     //initiating both tabs and set text to it. 
     ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A"); 
     ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B"); 

    //create the two fragments we want to use for display content 
     Fragment PlayerFragment = new AFragment(); 
     Fragment StationsFragment = new BFragment(); 

    //set the Tab listener. Now we can listen for clicks. 
     PlayerTab.setTabListener(new MyTabsListener(PlayerFragment)); 
     StationsTab.setTabListener(new MyTabsListener(StationsFragment)); 

    //add the two tabs to the actionbar 
     actionbar.addTab(PlayerTab); 
     actionbar.addTab(StationsTab); 
    } 

    class MyTabsListener implements ActionBar.TabListener { 
     public Fragment fragment; 

     public MyTabsListener(Fragment fragment) { 
     this.fragment = fragment; 
     } 

     @Override 
     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 
      if (fragment == null) { 
       Log.v(TAG, "fragment is null"); 
      } 

      if (ft == null) { 
       Log.v(TAG, "fragment TRANSACTION is null"); 
      } 

      ft.replace(R.id.fragment_container, fragment);   
     } 

     @Override 
     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 
      Toast.makeText(TabActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show(); 

     } 

    } 
} 

Hier ist AFragment.java

package com.pnet; 

import com.actionbarsherlock.app.SherlockFragment; 

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class AFragment extends SherlockFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.afragment, container, false); 
    } 

} 

Hier ist BFragment.java

package com.pnet; 

import com.actionbarsherlock.app.SherlockFragment; 

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class BFragment extends SherlockFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.bfragment, container, false); 
    } 

} 

Hier ist tab_activity.xml:

<LinearLayout android:layout_gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
<LinearLayout android:id="@+id/fragment_container" android:layout_height="match_parent" android:layout_width="match_parent"> 

</LinearLayout> 
</LinearLayout> 
012 Hier

ist afragment.xml:

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

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:gravity="center_vertical|center_horizontal" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="@string/hello_world"/> 


</LinearLayout> 

Hier ist bfragment.xml

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

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:gravity="center_vertical|center_horizontal" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="@string/tab_navigation_content"/> 


</LinearLayout> 

Wenn die Aktivität lädt sie löst die onTabSelected() Methode. Ich habe ein paar If-Anweisungen geschrieben, die Objekte auf Null überprüfen, die in das Protokoll schreiben, und es zeigt, dass das FragmentTransaction-Objekt null ist. Wer sieht wo ich falsch liege? Vielen Dank.

Antwort

Verwandte Themen