user5486846
user5486846

Reputation:

setSupportActionBar not working FragmentActivity

I'm trying to create a custom Toolbar on my FragmentActivity but at the time I launch the app it crashes on this lane :

setSupportActionBar(toolbar);

I have no more code, since I started to add this Toolbar so I have this on my MainActivity

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.Toolbar;



public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); //LogCat is pointing this
    }
}

My Toolbar code is :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:iosched="http://schemas.android.com/apk/res-auto"
    style="@style/HeaderBar"
    iosched:theme="@style/ActionBarThemeOverlay"
    iosched:popupTheme="@style/ActionBarPopupThemeOverlay"
    android:id="@+id/toolbar_actionbar"
    iosched:titleTextAppearance="@style/ActionBar.TitleText"
    iosched:contentInsetStart="?actionBarInsetStart"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize" />

What I'm doing wrong?

Upvotes: 5

Views: 9858

Answers (3)

James Smith
James Smith

Reputation: 75

I know this is rather late, but for the sake of those who really want to use FragmentActivity, you can add something like:

((AppCompatActivity)getApplicationContext()).setSupportActionBar(toolbar);

for Fragment:

((AppCompatActivity)getActivity).setSupportActionBar(toolbar);

Hope this is the answer that you're looking for.

Upvotes: 0

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

You need to extend the AppcompatActivity:

MainActivity extends AppcompatActivity

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

What I'm doing wrong?

You are extending FragmentActivity and this is the problem....

You should change it to AppCompatActivity

Your MainActivity should look like this : public class MainActivity extends AppCompatActivity

Also don't forget to import its libraries :

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

Upvotes: 9

Related Questions