下面是我为ViewPager实现并添加PagerSlidingTabStrip的代码。但我的viewpager既没有显示ActionBar也没有显示PagerSlidingTabStrip。如果我尝试在onResume()应用程序崩溃中添加actionBar.show(),显示空指针异常。
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>
答案 0 :(得分:0)
从您正在使用的PagerSlidingTabStrip
项目的github页面:
所以,你的布局应该是:
<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>