android – can I use a compatible action bar AND a fragment in the same activity?-ThrowExceptions

Exception or error:

OK, I’ve been extending my activities to

extends ActionBarActivity

My main menu page

extends FragmentActivity

when I change it to extend ActionBarActivity so I can see my action bar title on the main menu, it throws up and gives me the “Error Inflating Class Fragment” error.

Is there any way around this?

How to solve:

ActionBarActivity extends android.support.v4.app.FragmentActivity, so ActionBarActivity serves both purposes.

###

Is your code from the Action Bar compatibility example that comes with the SDK? If so just change the ActionBarActivity class in that example to extend FragmentActivity (from the compatibility library I assume) instead of Activity.

###

Experienced same problem and the solution, as mentioned in other responses, is to edit your ActionBarActivity class.

This class is defined in your code (com.google.android.actionbarcompat). The only thing you have to do is change extends Activity to extends FragmentActivity from the android.support.v4.app.FragmentActivity.

###

Do you have menu items for your Fragments? Use your own app namespace instead of android for app:showAsAction and app:actionLayout, etc.

To add your namespace to the XML use this line in the menu tag

xmlns:app="http://schemas.android.com/apk/res-auto"

E.g.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_settings" app:showAsAction="ifRoom" android:title="@string/menu_settings" android:orderInCategory="100" />
</menu>

Note the app:showAsAction rather than android:showAsAction

Leave a Reply

Your email address will not be published. Required fields are marked *