GetSupportActionBar return null

2019-07-04 06:12发布

问题:

After I started my second Activity, there isn't the ActionBar. When I call GetSupportActivity, it returns null. Why? I have minSdkVersion 10 and targetSdkVersion 15.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15"
        />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.Light" 
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
            android:windowSoftInputMode="stateHidden"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".ShowMusic2"
            android:label="Search Results">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>

</manifest>

This is the OnCreate of my second activity (ShowMusic2). It is a ListActivity.

public class ShowMusic2 extends SherlockListActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        showMusic = getIntent();
        pkgName = getPackageName();
        html = (String)showMusic.getStringExtra(pkgName + ".html");     
        new populateListView().execute(songs.toArray(new Song[songs.size()]));
        adapter =new SongAdapter(this,R.layout.list_item1, songs.toArray(new Song[songs.size()]));
        setListAdapter(adapter);
    }

回答1:

I havent played around with SherlockListActivity too much...but here's a shot in the dark....try moving

   getSupportActionBar().setDisplayHomeAsUpEnabled(true);

into your onStart method override instead of the onCreate



回答2:

for the problem getSupportActionBar() always return NULL That is because you set theme NoActionBar in AndroidManifest.xml

Theme.Sherlock.Light.NoActionBar

If your application does not use action bar then no need use that method.



回答3:

if you're using toolbar in a separate xml file without any layout, then getSupportActionBar() will return null. To avoid this error, just place your toolbar inside a layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <!-- Toolbar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:theme="@style/AppTheme.CustomThemeOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap" />

</LinearLayout>

put below code in your style.xml file

<style name="AppTheme.CustomThemeOverlay" parent="ThemeOverlay.AppCompat.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

This solution worked for me. And I hope this may help someone. :)