简体   繁体   中英

why I am getting actionbar null in android?

I am trying to make tab bar in android using this link http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/

when I run my project I am getting error on this line

ActionBar actionBar = getActionBar();

I am getting null value from here and getting null pointer exception could you please tell me how to remove this error

I am check out put on android 4.4.2

here is my code

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>

要使用您在此行中使用的AppCompat主题parent="Theme.AppCompat.Light.DarkActionBar"您必须使活动从AppCompatActivity扩展

public class MainActivity extends AppCompatActivity {

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

You have to define various style to support multiple version :

values -> styles.xml

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

values-v11 -> styles.xml

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

values-v14 -> styles.xml

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

There is some themes that can not be used in old SDKs like "@android:style/Theme.Holo.Light.DarkActionBar" is not supported before SDKs version 14.

To allow your app to support minimum specific version of SDK you could add the following under element:

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

To specify min SDK version in AndroidStudio, you could by using app's Gradle file.

android{
  defaultConfig{
    minSdkVersion 14
    targetSdkVersion 21
  }
}

The main reason for that is using themes that are not supporting ActionBar:

In manifest file add the following either in your target activity or application element (if you want to unify the theme over whole application)

Examples of themes that are supporting action bar "Theme.AppCompat.Light" or "Theme.Holo.Light" .

android:theme="@android:style/Theme.Holo.Light"

It is better to put all styles in styles.xml and use it everywhere using "@style/themName" so the previous one will be

android:theme="@style/AppTheme"

and styles.xml will have the following:

<style name="AppTheme" parent="Theme.AppCompat.Light">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM