cannot find symbol method getSupportActionBar()

二次信任 提交于 2019-12-24 14:37:58

问题


I know this question has been posted plenty of times, but I still unable to solve even i have tried many solution from 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));
        }

Error is on this line actionBar = (ActionBarActivity)getApplicationContext().getSupportActionBar();

Error

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

回答1:


Let Edit extend AppCompatActivity instead FragmentActivity. AppCompatActivity extends FragmentActivity and adds support for the ActionBar. You can read more here




回答2:


you need to change FragmentActivity to ActionBarActivity because this method is not provide in FragmentActivity so change.

public class mainActivity extends ActionBarActivity

check this Link




回答3:


Replace

public class Edit extends FragmentActivity implements ActionBar.TabListener

with

public class Edit extends ActionBarActivity implements ActionBar.TabListener

and you line

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


来源:https://stackoverflow.com/questions/34102696/cannot-find-symbol-method-getsupportactionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!