0
votes

I am implementing action bar with support library in my app. Min api level of my app is 8 and max is 19. In main activity class, I used android.app.ActionBar to import action bar, but it shows following error on getting action bar "Call requires API level 11 (current min is 8): android.app.Activity#getActionBar". So I changed import to 'android.support.v7.app.ActionBar' for action bar, now error changed to this ' cannot convert from android.app.ActionBar to android.support.v7.app.ActionBar'. My main activity code is as follow.

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
    Tab = (ViewPager)findViewById(R.id.pager); 
    Tab.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                  actionBar = getActionBar();
                  actionBar.setSelectedNavigationItem(position);                    }
            });
    Tab.setAdapter(TabAdapter);
    actionBar = getActionBar();
    //Enable Tabs on Action Bar
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener(){
  @Override
  public void onTabReselected(android.app.ActionBar.Tab tab,
      FragmentTransaction ft) {
    // TODO Auto-generated method stub
  }
  @Override
   public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
          Tab.setCurrentItem(tab.getPosition());
      }
  @Override
  public void onTabUnselected(android.app.ActionBar.Tab tab,
      FragmentTransaction ft) {
    // TODO Auto-generated method stub
  }};
  //Add New Tab
  actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
  actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
  actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
}
}

android manifest is

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.learn2crack.tab"
 android:versionCode="1"
 android:versionName="1.0" >
 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.learn2crack.tab.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>
 </manifest>

main_activity is

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

windows_frag.xml is

<?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:gravity="center"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="50sp"/>
</LinearLayout>

One of my fragment is

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Windows extends Fragment {
@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
    View windows = inflater.inflate(R.layout.windows_frag, container, false);
      ((TextView)windows.findViewById(R.id.textView)).setText("Windows");
      return windows;
 }}

Kindly help me out. I have also implemented following tutorial but error remain same.

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

Please don't suggest ActionBarSherlock. I want to know my issue before trying other library.

1
Clean and rebuild it!mmlooloo
I did many times but no success.. Request to everyone, if you view this and can't find the issue, then please vote it up so someone else may be able to find the bug and I can get answer. thanksK.Liaqat

1 Answers

1
votes

if you are using android.support.v7.app.ActionBar support package, you must use

actionBar = getSupportActionBar();

and change your activity like this

public class MainActivity extends ActionBarActivity
                          implements ActionBar.TabListener {
    // your code goes here
}