自定义窗口标题和ActionBar

时间:2013-01-21 12:03:28

标签: java android

为什么getActionBar()返回null?

我的应用中有自定义窗口标题。 在我的AndroidManifest.xml文件下面

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.example.map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/customTheme" >
        <activity
            android:name="com.example.map.MainActivity"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.map.MapActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.example.map.PersonalInformationActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="MY_KEY" />
    </application>
</manifest>

MainActivity的问题。它适用于ActionBar Tabs + Fragments(下面的代码)。在其他活动中,一切正常。

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_header); 

        final ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        String label1 = getResources().getString(R.string.label1);
        Tab tab = actionBar.newTab();
        tab.setText(label1);
        TabListener<Login> tl = new TabListener<Login>(this, label1, Login.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);

        String label2 = getResources().getString(R.string.label2);
        tab = actionBar.newTab();
        tab.setText(label2);
        TabListener<Registration> tl2 = new TabListener<Registration>(this, label2, Registration.class);
        tab.setTabListener(tl2);
        actionBar.addTab(tab);

    }

    private class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;

        public TabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Usually do nothing.

            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

如果您想使用ActionBar,则需要ActionBar 请参阅下面的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // The Action Bar is a window feature. The feature must be requested
    // before setting a content view.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.main);

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}

答案 1 :(得分:0)

API级别11中引入了

getActionBar()。在您的清单中,您声称您的应用也将在API级别8上运行。它不会。

Here是调用可能在API&gt; = 11的设备上返回null的众多原因。

答案 2 :(得分:0)

我有类似的问题,我曾经在下面的行上得到错误,因为 我在设置内容视图之前请求自定义标题功能。

setContentView(R.layout.userlayout);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)

首先请求功能并且问题消失

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)
setContentView(R.layout.userlayout);

希望这是你想要的解决方案..