Search code examples
androidandroid-viewpagerpagerslidingtabstrip

ViewPager neither shows ActionBar nor PagerSlidingTabStrip


Below is the code which i have implemented for ViewPager and adding PagerSlidingTabStrip.But my viewpager neither shows ActionBar nor shows PagerSlidingTabStrip.If i try to add actionBar.show() inside onResume() app crashes showing null pointer exception.

import android.app.Fragment;
import android.os.Bundle;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


import com.astuetz.PagerSlidingTabStrip;
     public class MainActivity extends FragmentActivity {
                private android.support.v4.app.Fragment currentFragment;

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


                    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
                    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
                    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
                    tabs.setViewPager(pager);

                }

                private class MyPagerAdapter extends FragmentPagerAdapter {
                    private String[] titles={"Titleme", "Titletos","Titleos", "Titles","MeTitle5","Title6","Title7","Title8"};
                    public MyPagerAdapter(FragmentManager fm) {
                        super(fm);
                    }
                    @Override
                    public CharSequence getPageTitle(int position) {
                        return titles[position];
                    }
                    @Override
                    public android.support.v4.app.Fragment getItem(int pos) {
                        Bundle args = new Bundle();

                        switch (pos) {

                            case 0:
                                currentFragment = new HomeFragment();




                                break;
                            case 1:

                                currentFragment = new HomeFragment5();




                                break;
                            case 2:

                                currentFragment = new HomeFragment4();



                                break;
                            case 3:

                                currentFragment = new HomeFragment3();



                                break;
                            case 4:

                                currentFragment = new HomeFragment4();



                                break;
                            case 5:
                                currentFragment = new ChlHome();


                                break;
                            case 6:
                                currentFragment = new GiFragment();
                                break;
                            case 7:
                                currentFragment = new FlList();
                                break;
                            default:
                        }
                        return currentFragment;
                    }

                    @Override
                    public int getCount() {
                        return 8;
                    }       
                }
            }

xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/main_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/viewPager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dip" />
            />
        </android.support.v4.view.ViewPager>
    </RelativeLayout>

Solution

  • From the github page of the PagerSlidingTabStrip project you are using:

    • Include the PagerSlidingTabStrip widget in your layout. This should usually be placed above the ViewPager it represents.

    So, you layout should be something along the lines of:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <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"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tabs"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>