【问题标题】:why am I getting an Theme related error in android studio?为什么我在 android studio 中收到与主题相关的错误?
【发布时间】:2015-10-18 13:53:24
【问题描述】:

我正在学习如何制作启动器的教程,但由于某种原因,当我将这行代码添加到我的主要活动中时,在清单中应用程序崩溃说我的应用程序已停止

android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar"

10-18 15:04:25.245  18122-18122/com.example.harrops.h20droidlauncher E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.harrops.h20droidlauncher, PID: 18122
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harrops.h20droidlauncher/com.example.harrops.h20droidlauncher.HomeLayout}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5598)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            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.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.harrops.h20droidlauncher.HomeLayout.onCreate(HomeLayout.java:13)
            at android.app.Activity.performCreate(Activity.java:5459)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5598)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

如果我删除该应用程序运行正常的代码行。

任何帮助都会很棒。一如既往地提前感谢。

【问题讨论】:

  • 将 logcat 添加到您的问题中

标签: android android-manifest android-theme


【解决方案1】:

要更改背景使用:

 android:background=" "

您选择了一张壁纸。

试试这个

android:background="#b6db49">

下面你说过你想使用主题。所以你必须进入清单并编辑那里的主题

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar">

【讨论】:

  • 是的,因为您正在尝试为背景添加样式。试试这个 android:background="#b6db49">
  • 但我希望能够使用设备壁纸
  • 您在壁纸中选择的内容。它重新设计了您应用程序上的整个主题。为此,您需要在清单中执行此操作。
【解决方案2】:

如果您在 Android Studio 中开始您的项目,默认情况下主要活动扩展 ActionBarActivity 需要 AppCompat 主题。将 Java 继承从 ActionBarActivity 更改为 Activity 并将主题保留在清单中。或者使用AppCompat 主题。

【讨论】:

  • 在 mainActivity java 文件中?
  • 我的意思是“YourActivity 扩展了 ActionBarActivity” -> “YourActivity 扩展了 Activity”。
猜你喜欢
  • 2020-12-12
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2018-03-10
相关资源
最近更新 更多