2

I want to implement an ActionBar with the Android v7 appcompat library to support the ActionBar for Android >= 2.1

My app starts with the MainActivity which contains a dark Actionbar, some information and a start button.

The next activity is the MenuActivity which contains also the dark Actionbar and some ActionBar Tabs which you can swipe


This is my manifest.xml with the DarkActionBar theme:

<application
    android:icon="@mipmap/ic_launcher"
    android:label="Hello World"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar">

    <activity
        android:name=".MainActivity"
        android:label="Hello World">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".gui.MenuActivity"
        android:label="Hello World" />

</application>

And this is the MenuActivity after the MainActivity which contains also the action bar additionally some navigation tabs:

package myapp.gui;

import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import myapp.R;

public class MenuActivity extends ActionBarActivity implements ActionBar.TabListener {

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        actionBar.addTab(actionBar.newTab().setText("Home").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Imprint").setTabListener(this));
    }

    ... 
}

If I start the app, the MainActivity works, but after clicking the start button and joining the MenuActivity, I get this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp.gui.MenuActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I've found some solutions, but no suitable for my problem. Can somebody help me?

I would also like to know if my solution is up to date or outdated?

Thank you :)

Stampy
  • 456
  • 7
  • 27

2 Answers2

3

Replace @android:style/Theme.Holo.Light.DarkActionBar in your AndroidManifest.xml with @style/Theme.AppCompat.Light.DarkActionBar

Make sure that you have the following dependency in your build.gradle: compile 'com.android.support:appcompat-v7:22.1.1'

Edit: Check the first comment by Knossos too!

Karim
  • 5,298
  • 3
  • 29
  • 35
  • 1
    Additionally, if you need your own styles in the theme also, you can set the parent of your theme to @style/Theme.AppCompat.Light.DarkActionBar – Knossos May 22 '15 at 09:18
  • If I do that, the ActionBar of the MainActivity disappears and the dropdown menu of the 3-dot-menu appears in the actionbar and not among it – Stampy May 22 '15 at 09:40
  • @Stampy Also replace `ActionBarActivity` with `AppCompatActivity` then. – Karim May 22 '15 at 09:42
  • That solved my first problem with the MainActivity, thanks. But the Dropdown menu of the actionbar appears still in the actionbar and not under it – Stampy May 22 '15 at 09:45
  • Please mark my answer as the correct answer since it answers your current question, and create a new question for your new issue. You can also post the link to this question here as a comment, and I'll be happy to help you with your new question as well. – Karim May 22 '15 at 09:48
  • 1
    @Karim Question asked: http://stackoverflow.com/questions/30397155/actionbar-three-dot-dropdown-opens-at-the-wrong-place – Stampy May 22 '15 at 12:51
  • @Stampy Just answered it :-) – Karim May 22 '15 at 13:55
  • @Karim I hope you will read that. I have one last problem with the new actionbar. It does not show the logo – Stampy May 25 '15 at 22:45
  • @Stampy Check this: http://stackoverflow.com/questions/26525229/toolbar-navigation-icon-never-set Let me know if it solves your issue. – Karim May 25 '15 at 23:02
-1

If you are extending ActionBarActivity in your MainActivity, you will have to change the parent theme in values-v11 also. So the style.xml in values-v11 will be -

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <style name="QueryTheme" parent="@style/Theme.AppCompat">
    <!-- Any customizations for your app running on devices with Theme.Holo here -->
    </style>
 </resources>

and Make sure you replace @android:style/Theme.Holo.Light.DarkActionBar in your AndroidManifest.xml with @style/Theme.AppCompat.Light.DarkActionBar

Kartheek
  • 7,104
  • 3
  • 30
  • 44