找不到符号方法getSupportActionBar()

时间:2015-12-05 08:04:00

标签: android tabs android-actionbar swipeview

我知道这个问题已经发布了很多次,但我仍然无法解决,即使我已经尝试了很多来自 Stack Overflow 的解决方案。

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import com.example.project.myapplication.API.InfoAPI;
import com.example.project.myapplication.Adapter.TabsFragmentPagerAdapter;


public class Edit extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private ActionBar actionBar;
    private TabsFragmentPagerAdapter tabsAdapter;
    private String[] activities = new String[]{"Information","Work Force","Work Details"};
    ListView listViewEdit;
    InfoAPI sqlcon;
    private MyDatabaseHelper dbHelper;
    private SQLiteDatabase database;
    private SimpleCursorAdapter dataAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsAdapter);
        actionBar = (ActionBarActivity)getApplicationContext().getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for(int i=0; i<3; i++){
            actionBar.addTab(actionBar.newTab().setText(activities[i]).setTabListener(this));
        }

错误在此行actionBar = (ActionBarActivity)getApplicationContext().getSupportActionBar();

错误

Error:(48, 63) error: cannot find symbol method getSupportActionBar()
Error:(48, 21) error: incompatible types: ActionBarActivity cannot be converted to ActionBar

3 个答案:

答案 0 :(得分:2)

Edit代替AppCompatActivity FragmentActivityAppCompatActivity扩展FragmentActivity并添加对ActionBar的支持。您可以阅读更多here

答案 1 :(得分:1)

您需要将FragmentActivity更改为ActionBarActivity 因为FragmentActivity没有提供这种方法所以改变。

public class mainActivity extends ActionBarActivity

检查此Link

答案 2 :(得分:1)

替换

public class Edit extends FragmentActivity implements ActionBar.TabListener

public class Edit extends ActionBarActivity implements ActionBar.TabListener

你行

actionBar = (ActionBar)getApplicationContext().getSupportActionBar();