java “您需要在此活动中使用 Theme.AppCompat 主题(或后代)。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47945222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"You need to use a Theme.AppCompat theme (or descendant) with this activity."
提问by Sean W
I am new to Android programming and I am running into a simple XML issue. I am in the process of trying to make one of my empty activities into a fullscreen activity. I tried adding this line of code android:theme="@android:style/Theme.NoTitleBar.Fullscreen" into my android manifest file, which ended up making my app crash on start.
我是 Android 编程的新手,我遇到了一个简单的 XML 问题。我正在尝试将我的一项空活动变成全屏活动。我尝试将这行代码 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 添加到我的 android 清单文件中,这最终使我的应用程序在启动时崩溃。
If this also helps, my java file for the affected activity extends the AppCompatActivity. I saw some other posts that mentioned that this would create issues but I have not been able to fix my problem.
如果这也有帮助,我的受影响活动的 java 文件将扩展 AppCompatActivity。我看到其他一些帖子提到这会产生问题,但我无法解决我的问题。
Please help me fix this issue while still making the activity fullscreen. Any help is appreciated. Thanks!
请帮助我解决此问题,同时仍使活动全屏显示。任何帮助表示赞赏。谢谢!
XML
XML
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light">
<activity android:name=".MainActivity"
android:label="Marks Calculator">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.APP_CALCULATOR" />
</intent-filter>
</activity>
<activity android:name=".Home_Activity"
android:label="Finite Time Manager">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
<activity
android:name=".Welcome_Activity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Error logcat
错误日志
12-22 12:21:30.214 32475-32475/com.managergmail.time.finite.finitemanager02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.managergmail.time.finite.finitemanager02, PID: 32475
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.managergmail.time.finite.finitemanager02/com.managergmail.time.finite.finitemanager02.Welcome_Activity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5763)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.managergmail.time.finite.finitemanager02.Welcome_Activity.onCreate(Welcome_Activity.java:17)
at android.app.Activity.performCreate(Activity.java:6280)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5763)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
回答by Nongthonbam Tonthoi
Your Welcome_Activity
probably extends AppCompatActivity
so the theme should be appcompat theme.
您Welcome_Activity
可能会扩展,AppCompatActivity
因此主题应该是 appcompat 主题。
In your styles.xml file put this:
在你的styles.xml 文件中输入:
<style name="AppTheme.FullScreen" parent="@style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
Now you can use this theme:
现在你可以使用这个主题:
<activity
android:name=".Welcome_Activity"
android:theme="@style/AppTheme.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This will apply fullscreen theme for this particular activity. If you want full screen for whole application you can just replace the application theme in manifest with this theme.
这将应用此特定活动的全屏主题。如果您希望整个应用程序全屏显示,您只需使用此主题替换清单中的应用程序主题即可。
回答by Merthan E
The theme you currently selected (in the welcome activity) isn′t a appcompat theme. This one should for example work: '
您当前选择的主题(在欢迎活动中)不是 appcompat 主题。例如,这个应该工作:'
@style/Theme.AppCompat.Light.NoActionBar.FullScreen
@style/Theme.AppCompat.Light.NoActionBar.FullScreen