1

我正在尝试在片段中创建滑动选项卡。通过参考链接中的教程。我正在使用ViewPager它。以下是我的代码:-

   public class A extends Fragment {


    private ViewPager pager;
    private TabsAdapter mTabsAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {

        // TODO Auto-generated method stub
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.one_fragment,
                null);

        pager = new ViewPager(getActivity());
        pager.setId(R.id.pager);
        getActivity().setContentView(pager);

        final ActionBar bar = getActivity().getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mTabsAdapter = new TabsAdapter(getActivity(), pager);
        mTabsAdapter.addTab(bar.newTab().setText("Mixed"), MixedFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Income"), MixedFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Expense"), MixedFragment.class, null);


        return v;
    }


}

当我运行应用程序时,它会在

 final ActionBar bar = getActivity().getActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

请任何人告诉我如何解决这个问题。或者给我一些示例代码来在里面创建滑动标签Fragment(不是FragmentActivity)。提前致谢。

下面是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hm.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

     <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light">
        <activity
            android:name="com.hm.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

以下是我的 logcat 显示的内容:

   FATAL EXCEPTION: main
java.lang.NullPointerException
at com.hm.A.onCreateView(A.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1514)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

3

getActionBar()可以返回null。

公共动作条getActionBar()

在 API 级别 11 中添加检索对此活动的 ActionBar 的引用。

返回 Activity 的 ActionBar,如果没有,则返回 null

您可能正在为活动使用非操作栏主题。

有一个动作栏主题

android:theme="@android:style/Theme.Holo.Light"
于 2014-04-26T06:38:20.970 回答