我正在尝试在片段中添加滑动选项卡。这是标签布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark" />
<com.example.materialdrawerfragment.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
和Fragment类,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_cont, container, false);
pager = (ViewPager) getActivity().findViewById(R.id.viewpager);
slidingTabLayout = (SlidingTabLayout) getActivity().findViewById(R.id.sliding_tabs);
pager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager(), titles));
slidingTabLayout.setViewPager(pager);
return v;
}
我得到的错误是,
04-11 00:40:31.759: E/AndroidRuntime(11107): FATAL EXCEPTION: main
04-11 00:40:31.759: E/AndroidRuntime(11107): java.lang.NullPointerException
04-11 00:40:31.759: E/AndroidRuntime(11107): at com.example.materialdrawerfragment.MyFragment.onCreateView(MyFragment.java:54)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.os.Handler.handleCallback(Handler.java:615)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.os.Looper.loop(Looper.java:137)
04-11 00:40:31.759: E/AndroidRuntime(11107): at android.app.ActivityThread.main(ActivityThread.java:4931)
04-11 00:40:31.759: E/AndroidRuntime(11107): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 00:40:31.759: E/AndroidRuntime(11107): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 00:40:31.759: E/AndroidRuntime(11107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-11 00:40:31.759: E/AndroidRuntime(11107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-11 00:40:31.759: E/AndroidRuntime(11107): at dalvik.system.NativeStart.main(Native Method)