添加" @android:style / Theme.Dialog"后,我的Android应用程序崩溃了在AndroidManifest.xml中

时间:2014-07-20 13:04:19

标签: java android xml

实际上,我的应用程序是本书的一个示例:开始Android 4开发。 它非常简单 - 我只想将一个对话框主题应用于一个活动。

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代码:

<?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的:

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

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

2 个答案:

答案 0 :(得分:3)

您遇到此问题的原因是您尝试应用对话框主题的活动正在扩展ActionBarActivity,这需要应用AppCompat主题。

所以改变

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

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

有关详细信息,请参阅You need to use a Theme.AppCompat

答案 1 :(得分:0)

你试过像

这样的事吗?
<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog" >

将对话框主题设置为活动,它将起作用

如果问题仍然存在,请发布logcat