带有 appcompat 库 v7 的 ActionBar(ava.lang.IllegalStateException:您需要使用 Theme.AppCompat 主题)
ActionBar with appcompat library v7 (ava.lang.IllegalStateException: You need to use a Theme.AppCompat theme)
我想用 Android v7 appcompat 库实现一个 ActionBar 以支持 Android >= 2.1
的 ActionBar
我的应用程序从 MainActivity 开始,它包含一个深色操作栏、一些信息和一个开始按钮。
下一个 activity 是 MenuActivity,它还包含深色 Actionbar 和一些可以滑动的 ActionBar Tabs
这是我的 manifest.xml 和 DarkActionBar 主题:
<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>
这是 MainActivity 之后的 MenuActivity,它还包含操作栏和一些导航选项卡:
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));
}
...
}
如果我启动应用程序,MainActivity 工作,但在单击开始按钮并加入 MenuActivity 后,我收到此错误:
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.
我找到了一些解决方案,但没有适合我的问题。有人可以帮助我吗?
我还想知道我的解决方案是最新的还是过时的?
谢谢:)
将 AndroidManifest.xml 中的 @android:style/Theme.Holo.Light.DarkActionBar
替换为 @style/Theme.AppCompat.Light.DarkActionBar
确保您的 build.gradle 中有以下依赖项:compile 'com.android.support:appcompat-v7:22.1.1'
编辑: 也检查 Knossos 的第一条评论!
如果您在 MainActivity
中扩展 ActionBarActivity
,您还必须更改 values-v11
中的父主题。
所以 values-v11
中的 style.xml
将是 -
<?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>
并确保将 AndroidManifest.xml
中的 @android:style/Theme.Holo.Light.DarkActionBar
替换为 @style/Theme.AppCompat.Light.DarkActionBar
我想用 Android v7 appcompat 库实现一个 ActionBar 以支持 Android >= 2.1
的 ActionBar我的应用程序从 MainActivity 开始,它包含一个深色操作栏、一些信息和一个开始按钮。
下一个 activity 是 MenuActivity,它还包含深色 Actionbar 和一些可以滑动的 ActionBar Tabs
这是我的 manifest.xml 和 DarkActionBar 主题:
<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>
这是 MainActivity 之后的 MenuActivity,它还包含操作栏和一些导航选项卡:
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));
}
...
}
如果我启动应用程序,MainActivity 工作,但在单击开始按钮并加入 MenuActivity 后,我收到此错误:
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.
我找到了一些解决方案,但没有适合我的问题。有人可以帮助我吗?
我还想知道我的解决方案是最新的还是过时的?
谢谢:)
将 AndroidManifest.xml 中的 @android:style/Theme.Holo.Light.DarkActionBar
替换为 @style/Theme.AppCompat.Light.DarkActionBar
确保您的 build.gradle 中有以下依赖项:compile 'com.android.support:appcompat-v7:22.1.1'
编辑: 也检查 Knossos 的第一条评论!
如果您在 MainActivity
中扩展 ActionBarActivity
,您还必须更改 values-v11
中的父主题。
所以 values-v11
中的 style.xml
将是 -
<?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>
并确保将 AndroidManifest.xml
中的 @android:style/Theme.Holo.Light.DarkActionBar
替换为 @style/Theme.AppCompat.Light.DarkActionBar