2

I want to add Tabs with ActionSherlockBar So i used this code from some (resource) and used the same code like i am giving. but after all the codes i am still not getting what i want. Even it is not showing any type of error Help me out Thank you for your suggestions

TabListener :

public class TabListener<T extends Fragment> implements ActionBar.TabListener{
  private TabFragment mFragment;
  private final Activity mActivity;
  private final String mTag;
  private final Class<T> mClass;

  public TabListener(Activity activity, String tag, Class<T> clz) {
    mActivity = activity;
    mTag = tag;
    mClass = clz;
  }

  public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // Check if the fragment is already initialized
    if (mFragment == null) {
      // If not, instantiate and add it to the activity
      mFragment = (TabFragment) Fragment.instantiate(
                        mActivity, mClass.getName());
      mFragment.setProviderId(mTag); // id for event provider
      ft.add(android.R.id.content, mFragment, mTag);
    } else {
      // If it exists, simply attach it in order to show it
      ft.attach(mFragment);
    }

  }

  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if (mFragment != null) {
      // Detach the fragment, because another one is being attached
      ft.detach(mFragment);
    }
  }

  public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // User selected the already selected tab. Usually do nothing.
  }
}

TabFragement :

public class TabFragment extends SherlockFragment {
  // your member variables here
  @Override
  public View onCreateView(LayoutInflater inflater, 
                 ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.customer_home, container, false);
    // do your view initialization here
    return view;
  }

public void setProviderId(String mTag) {
    // TODO Auto-generated method stub

}

}

Customer_home (Working class):

public class Customer_home extends SherlockFragmentActivity {
  // store the active tab here
  public static String ACTIVE_TAB = "activeTab";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // add tabs
    Tab tab1 = actionBar.newTab()
              .setText("TabTitle1")
              .setTabListener(new TabListener<TabFragment>(
               this, "tab1", TabFragment.class));
    actionBar.addTab(tab1);

    Tab tab2 = actionBar.newTab()
           .setText("TabTitle2")
           .setTabListener(new TabListener<TabFragment>(
                this, "tab2", TabFragment.class));
    actionBar.addTab(tab2);

    // check if there is a saved state to select active tab
    if( savedInstanceState != null ){
      getSupportActionBar().setSelectedNavigationItem(
                  savedInstanceState.getInt(ACTIVE_TAB));
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    // save active tab
    outState.putInt(ACTIVE_TAB,
            getSupportActionBar().getSelectedNavigationIndex());
    super.onSaveInstanceState(outState);
  }
}

AndroidMainfest:

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Spajs" >
    <activity
        android:name=".SplashScreenActivity"
        android:noHistory="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"
android:label="@string/app_name"
            android:noHistory="true"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
    <activity android:name=".Login"
        android:noHistory="true"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/Theme.Sherlock.Dialog">
    </activity>
    <activity android:name=".Customer_home"
android:windowSoftInputMode="stateAlwaysHidden"/>
    <activity android:name=".TabFragment"/>
    <activity android:name=".TabListener"/>

Community
  • 1
  • 1
Parth Sharma
  • 359
  • 1
  • 4
  • 21
  • Well, isn't the answer in the link you gave at the beginning? Read the comments and the rest of the answers -check this one from the answers - http://stackoverflow.com/questions/7599816/creating-tabs-using-fragments-now-that-tabactivity-is-deprecated – g00dy Aug 06 '13 at 11:47
  • I already added the library in my project which they are talking about. Still no response. @g00dy – Parth Sharma Aug 06 '13 at 11:54
  • What is the problem ? Are you seeing the action bar with no tabs ? – IanB Aug 06 '13 at 11:56
  • 08-06 17:29:16.179: E/AndroidRuntime(13498): FATAL EXCEPTION: main 08-06 17:29:16.179: E/AndroidRuntime(13498): java.lang.NoClassDefFoundError: com.SPAJS.icab.Customer_home – Parth Sharma Aug 06 '13 at 12:00
  • have you add the Customer_home in your manifest? – Sharad Mhaske Aug 06 '13 at 12:05
  • Obviously! @SharadMhaske See above i have added mainfest code! – Parth Sharma Aug 06 '13 at 12:06
  • Ohk its done.! i caught the problem.. :) – Parth Sharma Aug 06 '13 at 12:11
  • Tab tab1 = actionbar.newTab().setText(R.string.protection); tab1.setTabListener(new MyTablistener(this, getString(R.string.protection), Protection.class)); Tab tab2 = actionbar.newTab().setText(R.string.threat_log); tab2.setTabListener(new MyTablistener(this, getString(R.string.threat_log), ThreatLog.class)); Tab tab3 = actionbar.newTab().setText(R.string.operation); tab3.setTabListener(new MyTablistener(this, getString(R.string.operation), Operation.class)); actionbar.addTab(tab1); actionbar.addTab(tab2); actionbar.addTab(tab3); – Sharad Mhaske Aug 06 '13 at 12:12
  • the above code work for me. – Sharad Mhaske Aug 06 '13 at 12:12

0 Answers0