gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:53:27 27 4
gpt4 key购买 nike

实际上,我的应用程序是这本书中的示例:开始 Android 4 开发。这很简单 - 我只想将对话框主题应用于 Activity 。

主要 Activity .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>

日志猫:

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

为什么我的应用会崩溃?

最佳答案

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

所以改变

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

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

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

关于java - 在 AndroidManifest.xml 中添加 "@android:style/Theme.Dialog"后,我的 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850780/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com