ActionBar.setNavigationMode (ActionBar.NAVIGATION_MODE_TABS) is deprecated

I follow the tutorial https://www.youtube.com/watch?v=VVahIc8yENk and I get an error

java.lang.NullPointerException: attempt to call the virtual method 'void android.app.ActionBar.setNavigationMode (int)' on a null object Link

I use Android Studio to write this program and I tried from API 11 to 21 and none of them work.

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()); } } 
+6
source share
4 answers

I followed Vivz’s example on youtube, but when the method became outdated, I had to find another way. Instead of adding tabs to the action bar, try:

Change 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 exercise that you would like to implement on 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 want to tag your tabs, such as the Google Play store, with a moving indicator called tabs and navigate while the user scrolls through this library

 compile 'com.astuetz:pagerslidingtabstrip:1.0.1' 

And modify the viewpager layout file as follows:

 <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 get the desired effect.

Hope this helps !!!

+11
source

you can use " PagerSlidingTabStrip " instead of " setNavigationMode "

here is a tutorial and here is an example on github

+3
source
Finally, I found the answer. 1st your Android sdk
 <uses-sdk android:minSdkVersion="11" /> 

it should exceed 11. second, your topic should have an action bar, for example

 android:theme="Theme.AppCompat" 

third do not use this code in your activities.

 requestWindowFeature(Window.FEATURE_NO_TITLE); 

fourth, instead of import android.support.v4.app.ActionBar use import android.support.v7.app.ActionBar in your activity

fifth, change this value actionBar=getActionBar(); on actionbar=getSupportActionBar();

+1
source
 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference 

Your topic, apparently, is one that does not have an action bar. Since you are not using appcompat-v7 , be sure to use a theme that supports its own action bar, for example, one of the Theme.Holo series.

If you are trying to use appcompat-v7 instead, you need to inherit from ActionBarActivity , and you need to use getSupportActionBar() , not getActionBar() .

Also note that the action bar tabs have been deprecated since Android 5.0. You should consider another tab solution, such as a combination of ViewPager and PagerTabStrip .

0
source

Source: https://habr.com/ru/post/980386/


All Articles