25

呼び出しgetActionBarは戻りますnull。これは頻繁に報告されているので、他の人が使用した解決策を含めるようにしました:私minSdkVersion=11の、私はタイトルバーを持っています、そして私はgetActionBar後に電話をかけていsetContentViewます。また、私の活動は子供の活動ではありません。

setContentView(R.layout.main);

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

デバイスは、Android3.2を実行しているSamsungGalaxyTab10.1です。

アイデアや提案を事前に感謝します!

4

14 に答える 14

59

テーマまたは以下のコードのいずれかを介して、アクションバー(!=タイトルバー)の作成をリクエストする必要があるようです。

@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. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.main);

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

[ここ]からのコード

于 2012-04-05T15:21:07.527 に答える
11

これが機能するために満たす必要があるいくつかの条件があるようです。長い間私を困惑させたのはこれでした:

アクティビティがActivityを拡張していることを確認してください(ActionBarActivityではありません)。

public class MagicActivity extends Activity

アプリのマニフェストに次のようなものがあることを確認してください

<application
    ...
    android:theme="@android:style/Theme.Holo" >

アクティビティのレイアウトに

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    android:theme="@android:style/Theme.WithActionBar" >

-マーク

于 2014-11-17T13:29:56.020 に答える
10

getActionBar()getSupportActionBar( )に切り替えることで、すべてのエラーを解決したようです。getWindow()。requestFeature(Window.FEATURE_ACTION_BAR);を追加して、さまざまなテーマを試しました。、setContentView(view)が最初であり、他のすべてのものが前に表示されていることを確認しました。

于 2014-11-25T07:30:42.130 に答える
8

アクションバーには、アプリのタイトルが付いたテーマまたはアクティビティが必要です。アプリケーションまたはアクティビティをTheme.NOTITLEとしてスタイル設定していないことを確認してください。

<application
    android:name="com.xxx.yyy.Application"
    android:debuggable="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code


<activity
        android:name="com.xxx.yyy.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@style/Theme.NoTitle"  // remove this line if you have this in your code
        android:windowSoftInputMode="adjustResize|stateHidden" > 
于 2013-06-11T11:13:24.943 に答える
4

サポートライブラリを使用して、ActionBarActivity内のフラグメント内からActionBarを取得しようとしました。私はこれをしなければなりませんでした:

ActionBarActivity actionBarActivity = (ActionBarActivity)getActivity();
ActionBar actionBar = actionBarActivity.getSupportActionBar();

私の次の探求は、これを一般的にする方法を理解することです。たとえば、このコードは、ActionBarActivityによって使用されていることと、サポートライブラリを使用していることの両方をフラグメントが認識している必要があるため、汚い感じがします。getActivity().getActionBar()親アクティビティがActionBarActivityでない場合は、最初にチェックしてから上記のコードでClassCastExceptionをキャッチするメソッドである必要があると思います。

于 2014-11-05T18:18:44.243 に答える
2

API14以降のアプリケーションプロジェクトを作成する場合、values-14フォルダーがあります。フォルダが存在し、次のようなstyles.xmlファイルがあることを確認してください。

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
</style>

新しいプロジェクトを作成し、プロジェクトに追加のvaluesフォルダーが必要なかったため、このエラーが発生しました。そのため、valuesフォルダーを保持し、values-v11フォルダーとvalues-v14フォルダーを削除しました。ただし、API14以降のデフォルトのテーマでは、ActionBarを使用する場合、親XMLタグにActionBarが必要です。だから私はそれを間違って行うことができると思いました!これが誰かに役立つことを願っています。ただし、上記の回答で問題を解決する可能性が最も高いです。

于 2014-11-04T17:41:06.570 に答える
2

これで私の問題が修正されました:

android.support.v7.app.ActionBar ab = getSupportActionBar();
于 2016-07-01T11:26:54.697 に答える
1

本当の答えは@zaplと@HimanshuVirmaniの両方の組み合わせでなければなりません

Androidマニフェストの場合:

<application
    android:name="com.xxx.yyy.Application"
    android:debuggable="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code


<activity
        android:name="com.xxx.yyy.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@style/Theme.NoTitle"  // remove this line if you have this in your code
        android:windowSoftInputMode="adjustResize|stateHidden" > 

アクティビティの場合:

@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. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);
    // Hide the status bar for Android 4.1 and higher
    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}
于 2014-05-19T06:48:56.703 に答える
1

ACTIVITYではなくACTIONBARACTIVITYを拡張するようにしてくださいこれで問題が解決するはずです

于 2014-11-18T05:46:23.897 に答える
1

2つのことを変更するだけです1)マニフェストファイルの編集android:theme = "@ android:style / Theme.Holo">

2)フラグメントファイルはandroid:theme = "@ android:style/Theme.WithActionBar"を追加します

于 2015-05-21T09:27:40.063 に答える
1

システム要求の順序を変更します。

@Override
protected void onCreate(Bundle savedInstanceState) {

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    super.onCreate(savedInstanceState);    
    setContentView(R.layout.main);

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}
于 2015-07-27T13:01:28.803 に答える
0

sdk> = 14をターゲットとするアプリを開発していて、以前のバージョンのMaterialテーマもサポートするようにAppCompatを拡張している場合。戻るボタンのようにアクションバーからアイテムを非表示にする場合は、次のようにする必要があります。アクティビティ(フラグメントクラスではない)でgetActionBar()を使用すると、nullが返されます。Appsupportテーマの場合と同様に、getSupportAcionBar()はアクションバーを返します。

DevByteからフォローしたい場合は、ここにリンクがあります

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

    setContentView(R.layout.activity_detail);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);




}
于 2015-09-12T21:33:30.483 に答える
0

多分あなたの活動のスタイルはNoActionBarに基づいています

于 2017-11-02T15:36:18.837 に答える
0

ステップ01:

クラスの親を変更します。AppCompatActivityへのアクティビティ

すなわち:

public class BluetoothActivity extends AppCompatActivity {
//Your Code
}

ステップ02:

次のようにグローバル変数を作成します。(私はandroidxを使用しています。つまり、インポートは次のようになります)

androidx.appcompat.app.ActionBar actionBar;

ステップ03:

次のように暗いアクションバーを使用します。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your style -->
</style>

ステップ04:

getSupportActionBar();を割り当てます。変数に対して、onCreateメソッドでグローバルとして作成しました。

setContentView(R.layout.activity_bluetooth);
actionBar = getSupportActionBar(); //Create this After setContentView
actionBar.show(); //this is for Experimental process only

ステップ05:

これで、actionBarでやりたいことが何でもできます

すなわち

actionBar.setTitle("Profile");
于 2020-01-10T19:01:26.407 に答える