ViewPager既不显示动作条,也不PagerSlidingTabStrip [英] ViewPager neither shows ActionBar nor PagerSlidingTabStrip

查看:335
本文介绍了ViewPager既不显示动作条,也不PagerSlidingTabStrip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我已经实施了ViewPager和添加PagerSlidingTabStrip.But我viewpager既不显示动作条也显示PagerSlidingTabStrip.If我尝试添加actionBar.show()内onResume()的应用程序崩溃显示空指针异常code 。

 进口android.app.Fragment;
进口android.os.Bundle;进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.ViewPager;
进口com.astuetz.PagerSlidingTabStrip;
     公共类MainActivity扩展FragmentActivity {
                私人android.support.v4.app.Fragment currentFragment;                @覆盖
                保护无效的onCreate(捆绑savedInstanceState){
                    super.onCreate(savedInstanceState);
                    的setContentView(R.layout.activity_main);
                    ViewPager寻呼机=(ViewPager)findViewById(R.id.viewPager);
                    pager.setAdapter(新MyPagerAdapter(getSupportFragmentManager()));
                    PagerSlidingTabStrip标签=(PagerSlidingTabStrip)findViewById(R.id.tabs);
                    tabs.setViewPager(寻呼机);                }                私有类MyPagerAdapter扩展FragmentPagerAdapter {
                    私有String []标题= {Titleme,Titletos,Titleos,标题,MeTitle5,TITLE6,TITLE7,TITLE8};
                    公共MyPagerAdapter(FragmentManager FM){
                        超(FM);
                    }
                    @覆盖
                    公共CharSequence的getPageTitle(INT位置){
                        回到标题[位置]
                    }
                    @覆盖
                    公共android.support.v4.app.Fragment的getItem(INT POS){
                        捆绑ARGS =新包();                        开关(POS){                            情况下0:
                                currentFragment =新HomeFragment();
                                打破;
                            情况1:                                currentFragment =新HomeFragment5();
                                打破;
                            案例2:                                currentFragment =新HomeFragment4();                                打破;
                            案例3:                                currentFragment =新HomeFragment3();                                打破;
                            情况4:                                currentFragment =新HomeFragment4();                                打破;
                            情况5:
                                currentFragment =新ChlHome();
                                打破;
                            情况6:
                                currentFragment =新GiFragment();
                                打破;
                            案例7:
                                currentFragment =新FlList();
                                打破;
                            默认:
                        }
                        返回currentFragment;
                    }                    @覆盖
                    公众诠释的getCount(){
                        返回8;
                    }
                }
            }

XML

 <?XML版本=1.0编码=UTF-8&GT?;
    <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent机器人:layout_height =match_parent>
        <的FrameLayout
            机器人:ID =@ + ID / main_frame
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent/>        < android.support.v4.view.ViewPager
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
            机器人:ID =@ + ID / viewPager
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT>
        < com.astuetz.PagerSlidingTabStrip
            机器人:ID =@ + ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =48dip/>
            />
        < /android.support.v4.view.ViewPager>
    < / RelativeLayout的>


解决方案

PagerSlidingTabStrip 项目的GitHub的页面使用的是:


  • 包括在你的布局PagerSlidingTabStrip部件。这通常应放在ViewPager它上面再presents。

所以,你布局要沿着线的东西:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent机器人:layout_height =match_parent>
    < com.astuetz.PagerSlidingTabStrip
        机器人:ID =@ + ID /标签
        机器人:layout_width =match_parent
        机器人:layout_height =48dip/>
    < android.support.v4.view.ViewPager
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        机器人:ID =@ + ID / viewPager
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_below =@ + ID /标签
        机器人:layout_alignParentBottom =真/>
< / RelativeLayout的>

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>

解决方案

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>

这篇关于ViewPager既不显示动作条,也不PagerSlidingTabStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆