无法在android studio中更改主题

时间:2015-03-27 06:48:15

标签: java android xml android-theme

尝试使用溢出菜单中的三个按钮创建一个简单的溢出应用程序,每个按钮表示activity_main.xml的三种不同背景颜色 但每次我尝试更改应用程序的主题..应用程序崩溃时启动EMULATOR SHOWS UNFORTUNATELY APP STOPPED

我想制作应用Theme.Holo或任何显示溢出菜单按钮

的主题

该应用仅适用于以下代码中的以下修改:

manifest.xml文件

<application
android:theme="@style/AppTheme"
... >
</application>

style.xml文件

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

在这种情况下是应用程序窗口 溢出按钮不可见

上述两个代码中的任何更改(例如,在问题结束时提供的代码显示错误)使应用程序显示错误:打开应用程序时不安全溢出

Changing Theme.Holo on Android Manifest.xml file doesn't run on Android

中讨论了类似的问题

我也读过 https://developer.android.com/training/basics/actionbar/styling.html

但我无法理解解决方案......

以下所有应用程序文件都已复制.. 以下程序给出了错误,因为文件和样式已经编辑

MainActivity.java

package com.example.user.overflow;

import android.content.ClipData;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    RelativeLayout main_view = (RelativeLayout)     findViewById(R.id.main_view);

    switch (item.getItemId()){
        case R.id.menu_red:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.RED);
            return true;
        case R.id.menu_green:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.GREEN);
            return true;
        case R.id.menu_yellow:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.YELLOW);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main_view">

<TextView android:text="@string/hello_world"     android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

的strings.xml

<resources>
<string name="app_name">Overflow</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="red_string">red</string>
<string name="green_string">green</string>
<string name="yellow_string">yellow</string>
</resources>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo">
    <!-- Customize your theme here. -->
</style>
<style name="LightThemeSelector" parent="android:Theme.Holo">
    <!-- Customize your theme here. -->
</style>

</resources>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.overflow" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="android:Theme.Holo">

</style>

<!-- ActionBar styles -->

</resources>

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"    tools:context=".MainActivity">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/menu_red"
        android:orderInCategory="1"
        app:showAsAction="never"
        android:title="@string/red_string" />
    <item
        android:id="@+id/menu_green"
        android:orderInCategory="2"
        app:showAsAction="never"
        android:title="@string/green_string" />
    <item
        android:id="@+id/menu_yellow"
        android:orderInCategory="3"
        app:showAsAction="never"
        android:title="@string/yellow_string" />
</group>

</menu>

logcat错误:

03-27 16:54:09.004      817-817/com.example.user.overflow E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.overflow/com.example.user.overflow.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
        at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
        at com.example.user.overflow.MainActivity.onCreate(MainActivity.java:16)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您正在为所有活动使用此主题

android:theme="@style/CustomActionBarTheme" 

这是一个全息主题。

尝试转入

android:theme="@style/AppTheme" 

针对特定活动

答案 1 :(得分:0)

在styles.xml文件中进行一些编辑。在该文件中,您的父级当前是Base.Theme.AppCompat.Light.DarkActionBar,将其更改为Theme.AppCompat。 还将清单文件中的主题从style / CustomActionBarTheme更改为style / AppTheme。 现在,您可以轻松更改主题。