簡體   English   中英

為什么我在Android中獲得actionbar null?

[英]why I am getting actionbar null in android?

我正在嘗試使用此鏈接在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>

要使用您在此行中使用的AppCompat主題parent="Theme.AppCompat.Light.DarkActionBar"您必須使活動從AppCompatActivity擴展

public class MainActivity extends AppCompatActivity {

嘗試使用getSupportActionBar()而不是getActionBar()

您必須定義各種樣式以支持多個版本:

值-> styles.xml

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

值-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>

在SDK版本14之前,某些主題無法在舊的SDK中使用,例如"@android:style/Theme.Holo.Light.DarkActionBar"

為了允許您的應用支持最低特定版本的SDK,您可以在元素下添加以下內容:

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

要在AndroidStudio中指定SDK的最低版本,可以使用應用的Gradle文件。

android{
  defaultConfig{
    minSdkVersion 14
    targetSdkVersion 21
  }
}

這樣做的主要原因是使用了不支持ActionBar的主題:

在清單文件中,在目標活動或應用程序元素中添加以下內容(如果要在整個應用程序中統一主題)

支持操作欄"Theme.AppCompat.Light""Theme.Holo.Light"的主題示例。

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

最好將所有樣式放入styles.xml中,並使用"@style/themName"在所有地方使用它,因此上一個"@style/themName"將是

android:theme="@style/AppTheme"

and styles.xml將具有以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM