2
votes

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 :)

2

2 Answers

3
votes

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!

-1
votes

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