Search code examples
androidandroid-actionbar

getSupportActionBar error


I'm new to android development. I want to create my custom actionbar. I've encountered this problem: if I declare actionBar mActionBar = getActionBar(); my app crashes at runtime. But if I use getSupportActionBar() I can't compile with the follow error: incompatible types. I've tried all possible implementation but it's still not working. I'm using android studio. Here is my code:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setCustomView(R.layout.sample_search);
        mActionBar.setDisplayShowTitleEnabled(false);
    }


}

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.search_bar" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/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>

Solution

  • Make sure you have imported

    android.support.v7.app.ActionBar
    

    this and not this

    android.app.ActionBar