ViewPager doesn't show ActionBar and PagerSlidingTabStrip

Below is the code that I have applied for the ViewPager and adding the PagerSlidingTabStrip. But my viewer does not show ActionBar and does not show PagerSlidingTabStrip.If I try to add actionBar.show () to onResume () the application crashes showing the exception with an exclusive.

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>

      

+3


source to share


1 answer


From the github page of the project PagerSlidingTabStrip

you are using:

  • Include the PagerSlidingTabStrip widget in your layout. Typically this should be placed above the ViewPager.


So your layout should be something like:

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

      

0


source







All Articles