GetSupportActionBar return null

2019-07-04 05:20发布

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);
    }

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-04 06:11

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

查看更多
放我归山
3楼-- · 2019-07-04 06:13
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. :)

查看更多
女痞
4楼-- · 2019-07-04 06:15

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.

查看更多
登录 后发表回答