我正在尝试设置一个FragmentStatePagerAdapter
以在我的一个片段上创建一个可滚动的选项卡界面,类似于此示例。在我的活动中onCreate
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base);
setVersionString();
setupActionBar();
setupNavigationDrawer();
setupFragments();
}
我将内容视图设置为以下布局 ( R.layout.base
):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseActivity" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#FFF" />
</android.support.v4.widget.DrawerLayout>
然后我调用以下函数:
private void setupFragments() {
// Check that the activity is using the correct layout
if (findViewById(R.id.main_frame) != null) {
// Initialize the first fragment
MainFragment firstFrag = new MainFragment();
// Pass any extras from an intent to the Fragment
firstFrag.setArguments(getIntent().getExtras());
// Add the first Fragment to the screen
getSupportFragmentManager().beginTransaction()
.add(R.id.main_frame, firstFrag).commit();
tabsAdapter = firstFrag.getAdapter();
Log.d("Base.LOG", "tabsAdapter = " + tabsAdapter);
tabsAdapter.addTab(DummyFragment.class, null);
}
}
尝试将 a 设置FragmentStatePagerAdapter
为片段布局 ( ) 中视图的适配器R.layout.main
:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
我的片段的代码看起来是这样的:
private View mView;
private TabbedPagerAdapter mTabPageAdapter;
private ViewPager mTabPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Main.LOG", "onCreate called");
mView = inflater.inflate(R.layout.main, container, false);
Log.d("Main.LOG", "Layout inflated");
setupTabs();
Log.d("Main.LOG", "Tabs setup");
return mView;
}
@Override
public void onResume() {
super.onResume();
Log.d("Main.LOG", "onResume called");
}
/** Set up the ViewPager and Adapter to handle the primary tabs */
private void setupTabs() {
// Initialize the adapter
mTabPageAdapter = new TabbedPagerAdapter(getActivity(),
getActivity().getSupportFragmentManager());
// Initialize the view pager
mTabPager = (ViewPager) mView.findViewById(R.id.main_page);
Log.d("Main.LOG", "mTabPager = " + mTabPager);
Log.d("Main.LOG", "mTabPageAdapter = " + mTabPageAdapter);
mTabPager.setAdapter(mTabPageAdapter);
}
/* Accessor for TabbedPagerAdapter */
public TabbedPagerAdapter getAdapter() {
return mTabPageAdapter;
}
当我尝试添加带有NullPointerException
. 日志中的调试消息显示:
D/Base.LOG(27173): tabsAdapter = null
显示在任何片段的调试消息之前。在尝试访问它的任何视图或子类之前,如何确保布局已膨胀?