加入&QUOT后我的Andr​​oid应用程序崩溃; @android:风格/ Theme.Dialog"在AndroidManifest.xml

问题描述:

其实,我的应用程序是从书的样本:开始Android的4发展。
这很简单 - 我只想一个对话框主题应用到的活动。

Actually, my app is a sample from the book: Beginning Android 4 Development. It's very simple - I just want to apply a dialog theme to an activity.

MainActivity.java:

MainActivity.java:

package com.example.activity101;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

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.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

布局:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.activity101.MainActivity" >

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

</RelativeLayout>

这在AndroidManifest.xml中codeS:

This the AndroidManifest.xml codes:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog" >
        <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>

logcat的:

logCat:

07-20 21:38:16.627: D/dalvikvm(2211): Late-enabling CheckJNI
07-20 21:38:17.878: D/libEGL(2211): loaded /system/lib/egl/libGLES_android.so
07-20 21:38:17.948: D/libEGL(2211): loaded /system/lib/egl/libEGL_adreno200.so
07-20 21:38:18.008: D/libEGL(2211): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
07-20 21:38:18.018: D/libEGL(2211): loaded /system/lib/egl/libGLESv2_adreno200.so
07-20 21:38:18.629: D/memalloc(2211): /dev/pmem: Mapped buffer base:0x52145000 size:8007680 offset:5918720 fd:62
07-20 21:38:18.719: D/OpenGLRenderer(2211): Enabling debug mode 0
07-20 21:38:20.110: D/memalloc(2211): /dev/pmem: Mapped buffer base:0x52d35000 size:2088960 offset:0 fd:65
07-20 21:38:51.641: D/OpenGLRenderer(2211): Flushing caches (mode 0)
07-20 21:38:51.651: D/memalloc(2211): /dev/pmem: Unmapping buffer base:0x52145000 size:8007680 offset:5918720
07-20 21:38:51.651: D/memalloc(2211): /dev/pmem: Unmapping buffer base:0x52d35000 size:2088960 offset:0
07-20 21:39:00.279: D/libEGL(3284): loaded /system/lib/egl/libGLES_android.so
07-20 21:39:00.299: D/libEGL(3284): loaded /system/lib/egl/libEGL_adreno200.so
07-20 21:39:00.309: D/libEGL(3284): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
07-20 21:39:00.309: D/libEGL(3284): loaded /system/lib/egl/libGLESv2_adreno200.so
07-20 21:39:00.389: D/memalloc(3284): /dev/pmem: Mapped buffer base:0x52127000 size:33284096 offset:31195136 fd:62
07-20 21:39:00.409: D/OpenGLRenderer(3284): Enabling debug mode 0
07-20 21:39:00.539: D/memalloc(3284): /dev/pmem: Mapped buffer base:0x543e2000 size:2088960 offset:0 fd:65
07-20 21:40:19.016: D/OpenGLRenderer(3284): Flushing caches (mode 0)
07-20 21:40:19.016: D/memalloc(3284): /dev/pmem: Unmapping buffer base:0x52127000 size:33284096 offset:31195136
07-20 21:40:19.016: D/memalloc(3284): /dev/pmem: Unmapping buffer base:0x543e2000 size:2088960 offset:0
07-20 21:40:19.076: D/OpenGLRenderer(3284): Flushing caches (mode 1)
07-20 21:40:29.756: D/AndroidRuntime(4629): Shutting down VM
07-20 21:40:29.756: W/dalvikvm(4629): threadid=1: thread exiting with uncaught exception (group=0x40abe228)
07-20 21:40:29.787: E/AndroidRuntime(4629): FATAL EXCEPTION: main
07-20 21:40:29.787: E/AndroidRuntime(4629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity101/com.example.activity101.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.os.Looper.loop(Looper.java:154)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.ActivityThread.main(ActivityThread.java:4945)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at java.lang.reflect.Method.invoke(Method.java:511)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at dalvik.system.NativeStart.main(Native Method)
07-20 21:40:29.787: E/AndroidRuntime(4629): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at com.example.activity101.MainActivity.onCreate(MainActivity.java:12)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.Activity.performCreate(Activity.java:4531)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-20 21:40:29.787: E/AndroidRuntime(4629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
07-20 21:40:29.787: E/AndroidRuntime(4629):     ... 11 more

为什么我的应用程序崩溃?

Why does my app crashes?

您有这个问题的原因是因为你尝试应用对话框主题的活动正在扩展 ActionBarActivity 这就要求程序兼容性主题加以应用。

The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.

所以更改

android:theme="@android:style/Theme.Dialog"

android:theme="@style/Theme.AppCompat"

有关更多信息请参见You需要使用Theme.AppCompat