【问题标题】:Xamarin Android Theme.AppCompat error - at runtime onlyXamarin Android Theme.AppCompat 错误 - 仅在运行时
【发布时间】:2014-09-04 17:07:23
【问题描述】:

我有一个使用 v4 和 v7 支持库的应用。它在调试中运行得非常好,但现在我已经在发布模式下运行它,我收到与主题相关的错误。

“您需要在此活动中使用 Theme.AppCompat 主题(或后代)。”

这是我的应用顶部的声明,我确实声明了一个 AppCompat 主题:

   [Activity(Label = "My App", LaunchMode = LaunchMode.SingleTask, Theme="@style/Theme.AppCompat.Light", ScreenOrientation = ScreenOrientation.Portrait)]

我也在Manifest中声明:

<application android:icon="@drawable/logo" android:debuggable="false" android:label="My App" android:theme="@style/Theme.AppCompat.Light">

这是我的堆栈跟踪。任何帮助将不胜感激。

libc] WARNING: generic atexit() called from legacy shared library
[monodroid-gc] GREF GC Threshold: 46800
[dalvikvm-heap] Grow heap (frag case) to 9.750MB for 803952-byte allocation
[dalvikvm-heap] Grow heap (frag case) to 11.473MB for 1607840-byte allocation
[Adreno-EGL] <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
[OpenGLRenderer] Enabling debug mode 0
[Request] Warning: Sessionless Request needs token but missing either application ID or client token.
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>
[MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x000e7>
[MonoDroid] at Android.App.Activity.OnCreate (Android.OS.Bundle) <0x001f3>
[MonoDroid] at FrogPoint.Droid.MainActivity.OnCreate (Android.OS.Bundle) <0x0001f>
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005b>
[MonoDroid] at (wrapper dynamic-method) object.167c14b1-1233-456f-ab3e-1b59de9fa9bf (intptr,intptr,intptr) <0x00043>
[MonoDroid]   --- End of managed exception stack trace ---
[MonoDroid] java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
[MonoDroid]     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
[MonoDroid]     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
[MonoDroid]     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
[MonoDroid]     at frogpoint.droid.MainActivity.n_onCreate(Native Method)
[MonoDroid]     at frogpoint.droid.MainActivity.onCreate(MainActivity.java:41)
[MonoDroid]     at android.app.Activity.performCreate(Activity.java:5231)
[MonoDroid]     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
[MonoDroid]     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
[MonoDroid]     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
[MonoDroid]     at android.app.ActivityThread.access$800(ActivityThread.java:135)
[MonoDroid]     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
[MonoDroid]     at android.os.Handler.dispatchMessage(Handler.java:102)
[MonoDroid]     at android.os.Looper.loop(Looper.java:136)
[MonoDroid]     at android.app.ActivityThread.main(ActivityThread.java:5001)
[MonoDroid]     at java.lang.reflect.Method.invokeNative(Native Method)
[MonoDroid]     at java.lang.reflect.Method.invoke(Method.java:515)
[MonoDroid]     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
[MonoDroid]     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
[MonoDroid]     at dalvik.system.NativeStart.main(Native Method)
[mono] 
[mono] Unhandled Exception:
[mono] Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
[mono] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>
[mono] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x000e7>
[mono] at Android.App.Activity.OnCreate (Android.OS.Bundle) <0x001f3>
[mono] at FrogPoint.Droid.MainActivity.OnCreate (Android.OS.Bundle) <0x0001f>
[mono] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005b>
[mono] at (wrapper dynamic-method) object.167c14b1-1233-456f-ab3e-1b59de9fa9bf (intptr,intptr,intptr) <0x00043>
[mono] 
[mono]   --- End of managed exception stack trace ---
[mono] java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
[mono]  at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
[mono]  at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
[mono]  at android.support.v7.app.ActionBarActivit
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
[mono-rt] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>
[mono-rt] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x000e7>
[mono-rt] at Android.App.Activity.OnCreate (Android.OS.Bundle) <0x001f3>
[mono-rt] at FrogPoint.Droid.MainActivity.OnCreate (Android.OS.Bundle) <0x0001f>
[mono-rt] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005b>
[mono-rt] at (wrapper dynamic-method) object.167c14b1-1233-456f-ab3e-1b59de9fa9bf (intptr,intptr,intptr) <0x00043>
[mono-rt] 
[mono-rt]   --- End of managed exception stack trace ---
[mono-rt] java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
[mono-rt]   at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
[mono-rt]   at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
[mono-rt]   at android.support.v7.app.Act

【问题讨论】:

  • 您需要使用 API 14 或更高版本进行编译。
  • 谢谢 Cheesebaron,但我的 Target 框架已经是 4.4,最低设置为 4.3。所以我认为这不是问题,除非我遗漏了什么。
  • 你是在使用 ActionBarActivity 还是只使用 Activity?
  • 另外,如果你使用的是 4.3 及以上版本,为什么需要 Support v7?
  • 是的,非常好的问题,为了解决这个问题,我最终从 ActionbarActivity 更改为 Activity,并在我的片段中进行了所有其他必要的更改。这似乎已经做到了,我只是担心改变它在片段和支持包方面相当缺乏经验。感谢您的帮助 Cheesebaron。

标签: android xamarin runtime-error android-appcompat release-mode


【解决方案1】:

我在我的应用程序中遇到了同样的错误。对我有用的解决方案很简单。只需在您的活动标签中使用此主题

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

不要使用任何其他主题。一定会成功的。

【讨论】:

    【解决方案2】:

    以下是步骤: 转到属性-> Androidmanifest.xml 它将在 IDE 中打开文件 在应用程序主题中输入值作为@style/MagicTheme 保存 现在去 Resources-->Values -- 通过 style.xml 添加一个文件 在 style.xml 中编写以下代码

    <resources>
      <style name="MyTheme.Base" parent="Theme.AppCompat.Light">
      </style>
      <style name="MyTheme" parent="MyTheme.Base">
      </style>
      <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/pngfilename</item>
        <item name="android:windowNoTitle">true</item>  
        <item name="android:windowFullscreen">false</item>  
        <item name="android:windowContentOverlay">@null</item>  
        <item name="android:windowActionBar">true</item>  
      </style>
    </resources>
    

    【讨论】:

      【解决方案3】:

      正如我们在 OP 的 cmets 中讨论的那样。如果您的目标是 Android 4.3 及更高版本,则无需使用 Support v7。支持包旨在向后兼容不支持新 API 级别内容的 API 级别。比如ActionBar、Fragments等。但是由于API 13、Fragments和ActionBar都包含在内,所以只有以下API级别才需要支持v7。

      至于您遇到的崩溃,这已在早期版本的 Xamarin.Android 中修复,因此如果您没有使用任何较新版本,这可能是问题所在。最新的稳定版应该没问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-06
        • 2021-03-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多