为什么我在android中获得actionbar null?

时间:2015-08-24 08:14:36

标签: android android-layout

我正在尝试使用此链接在android中制作标签栏 http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/

当我运行我的项目时,我在这一行上收到错误

ActionBar actionBar = getActionBar();

我从这里得到空值并获得空指针异常,请你告诉我如何删除此错误

我退房时放在android 4.4.2

这是我的代码

package com.androidbegin.fragmenttabstutorial;

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2, Tab3;
    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();

        // Hide Actionbar Icon
        actionBar.setDisplayShowHomeEnabled(false);

        // Hide Actionbar Title
        actionBar.setDisplayShowTitleEnabled(false);

        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setIcon(R.drawable.tab1);
        Tab2 = actionBar.newTab().setText("Tab2");
        Tab3 = actionBar.newTab().setText("Tab3");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        Tab3.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
        actionBar.addTab(Tab3);
    }
}

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.naveen.tabfragment" >
    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="15" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

Style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

4 个答案:

答案 0 :(得分:0)

要使用您在此行AppCompat中使用的parent="Theme.AppCompat.Light.DarkActionBar"主题,您的活动必须延伸至AppCompatActivity

public class MainActivity extends AppCompatActivity {

答案 1 :(得分:0)

尝试使用getSupportActionBar()而不是getActionBar()

答案 2 :(得分:0)

您必须定义各种样式以支持多个版本:

值 - &gt; styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Light" />
</resources>

values-v11 - &gt; styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light" />
</resources>

values-v14 - &gt; styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
</resources>

答案 3 :(得分:0)

在SDK版本14之前,有些主题无法在private等旧版SDK中使用。

要允许您的应用支持最低特定版本的SDK,您可以在元素下添加以下内容:

"@android:style/Theme.Holo.Light.DarkActionBar"

要在AndroidStudio中指定min SDK版本,您可以使用应用程序的Gradle文件。

<uses-sdk android:minSdkVersion="14" />

主要原因是使用不支持ActionBar的主题:

在清单文件中,在目标活动或应用程序元素中添加以下内容(如果要在整个应用程序中统一主题)

支持操作栏android{ defaultConfig{ minSdkVersion 14 targetSdkVersion 21 } } "Theme.AppCompat.Light"的主题示例。

"Theme.Holo.Light"

最好将所有样式放在styles.xml中,并使用android:theme="@android:style/Theme.Holo.Light" 在任何地方使用它,以便前一个样式为

"@style/themName"

和styles.xml将具有以下内容:

android:theme="@style/AppTheme"