java – actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)已弃用

前端之家收集整理的这篇文章主要介绍了java – actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)已弃用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在按照教程 https://www.youtube.com/watch?v=VVahIc8yENk进行操作,我收到了错误消息

java.lang.NullPointerException: Attempt to invoke virtual method ‘void
android.app.ActionBar.setNavigationMode(int)’ on a null object
reference

我正在使用Android Studio编写这个程序,我已经尝试从API 11到21,但没有一个工作.

public class Tabtest extends FragmentActivity implements ActionBar.TabListener {

    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_test);

        actionBar=getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1=actionBar.newTab();
        tab1.setText("tab1");
        tab1.setTabListener(this);

        ActionBar.Tab tab2=actionBar.newTab();
        tab2.setText("tab2");
        tab2.setTabListener(this);

        ActionBar.Tab tab3=actionBar.newTab();
        tab3.setText("tab3");
        tab3.setTabListener(this);

        ActionBar.Tab tab4=actionBar.newTab();
        tab4.setText("tab4");
        tab4.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
        actionBar.addTab(tab4);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,FragmentTransaction ft) {
        Log.d("VIVZ","onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,"onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,"onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }
}

解决方法

我在youtube上关注Vivz示例,但是当该方法被弃用时,我必须找到另一种方法.尝试:而不是向操作栏添加标签

修改你的适配器:

public class CollectionPagerAdapter extends FragmentStatePagerAdapter {

    private String[] titles = {"Item 1","Item 2","Item 3" };

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch(i){
          case 0:
             return new FragmentA();
          case 1:
             return new FragmentB();
          case 2:
             return new FragmentC();        
        }
        return null;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

在您想要实现选项卡的活动中尝试

public class Tabtest extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_test);

    ViewPager pager = (ViewPager) findViewById(R.id.your_view_pager);
    pager.setAdapter(new CollectionPagerAdapter(getSupportFragmentManager()));

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);
}

现在,如果您想在选项卡名称下设置带有移动指示器的Google Play商店标签,并在用户滚动时移动,请编译此库

compile 'com.astuetz:pagerslidingtabstrip:1.0.1'

修改您的viewpager布局文件,如下所示:

<LinearLayout
  //obvIoUsly add width and height and other necessery stuff
  android:orientation="vertical">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<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" />


</LinearLayout>

然后你将获得预期的效果.

希望能帮助到你!!!

原文链接:https://www.f2er.com/java/128263.html

猜你在找的Java相关文章