Java actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) 已弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27726759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) deprecated
提问by RashaadBryan
I'm following the tutorial https://www.youtube.com/watch?v=VVahIc8yENkand I'm getting the error
我正在按照教程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
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.app.ActionBar.setNavigationMode(int)”
I'm using Android Studio to write this program and I have tried from API 11 to 21 and none of them work.
我正在使用 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, FragmentTransaction ft) {
Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
}
}
回答by CommonsWare
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
Your theme apparently is one that does not have an action bar. Since you are not using appcompat-v7
, make sure that you are using a theme that supports the native action bar, such as one of the Theme.Holo
series.
您的主题显然是没有操作栏的主题。由于您没有使用appcompat-v7
,请确保您使用的是支持本机操作栏的主题,例如Theme.Holo
系列之一。
If, instead, you are trying to use appcompat-v7
, you need to inherit from ActionBarActivity
and you need to use getSupportActionBar()
, not getActionBar()
.
相反,如果您尝试使用appcompat-v7
,则需要继承自,ActionBarActivity
并且需要使用getSupportActionBar()
,而不是getActionBar()
。
Also note that action bar tabs were deprecated as of Android 5.0. You should consider another tab solution, such as the combination of ViewPager
and PagerTabStrip
.
另请注意,自 Android 5.0 起,不推荐使用操作栏选项卡。你应该考虑另一个选项卡的解决方案,比如组合ViewPager
和PagerTabStrip
。
回答by CommonsWare
I was following Vivz example in youtube but when the method deprecated i had to find another way. Instead of adding tabs to the actionbar try:
我在 youtube 上关注 Vivz 示例,但是当该方法被弃用时,我不得不找到另一种方法。不要向操作栏添加选项卡,请尝试:
Modify your adapter:
修改您的适配器:
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];
}
}
And in the activity that you would like to implement the tabs try
在您想要实现选项卡的活动中尝试
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);
}
Now if you would like to style your tabs like Google Play store with a moving indicator under the tab name and move while the user scrolls compile this library
现在,如果您想在选项卡名称下使用移动指示器设置像 Google Play 商店一样的选项卡样式,并在用户滚动时移动,请编译此库
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
And modify your viewpager layout file like this:
并像这样修改您的 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>
And then you are going to have the desired effect.
然后你就会有想要的效果。
Hope it helps!!!
希望能帮助到你!!!
回答by Hojjat Mehri
finally i found answer. 1st your android sdk
最后我找到了答案。第一个你的安卓 sdk
<uses-sdk android:minSdkVersion="11" />
it must higher than 11. 2nd your theme must have actionbar like
它必须高于 11。第二你的主题必须有像这样的操作栏
android:theme="Theme.AppCompat"
3rd don't use this code in your activity
第三,不要在您的活动中使用此代码
requestWindowFeature(Window.FEATURE_NO_TITLE);
4th
instead of import android.support.v4.app.ActionBar
use import android.support.v7.app.ActionBar
in your activity
4th 而不是在您的活动中import android.support.v4.app.ActionBar
使用import android.support.v7.app.ActionBar
5th
change this actionBar=getActionBar();
to actionbar=getSupportActionBar();
5th 将此更改actionBar=getActionBar();
为actionbar=getSupportActionBar();
回答by David Hackro
you can use "PagerSlidingTabStrip" replacing "setNavigationMode"
您可以使用“ PagerSlidingTabStrip”替换“ setNavigationMode”
here a tutorialand here a examplein Github