gpt4 book ai didi

android - 如何在整个应用程序范围内使用 Android 的 AppCompat 主题?

转载 作者:行者123 更新时间:2023-11-29 00:13:02 25 4
gpt4 key购买 nike

IDE:Android Studio 1.1.0

ENV:使用 Android API v10 在连接 usb 的手机上进行调试。


我正在尝试使用 AppCompat 来支持 Android API v8 及更高版本。编译正常但在绘制 Activity 时崩溃,说:

lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

...但是,在 AndroidManifest.xml 中我有:

    <application
android:name=".objects.PikCMS"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/ThemeOverlay.AppCompat"
>

谢谢,请原谅我的挫败感。我一直在阅读很多相关但无用的信息。 }8,(


其他信息

  • 我不想强加一个特定的主题。
  • 我想避免为不同的 API 版本使用不同的文件。但是,我不介意将同一属性的不同版本并排添加。
  • 我在 v11 中引入的项目的 styles.xml 中有错误(例如“buttonBarStyle”和“showDividers”)。在阅读了有关该问题的信息后,我原以为 AppCompat 会为 pre11 API“填补空白”,但(在我的配置中)事实并非如此。这些目前都被注释掉了——直到我找到如何将它们与 AppCompat 一起使用。同样,我的布局文件也有问题——这些有问题的属性已被删除。
  • Activity 类:Login.java 扩展 :ActionBarActivity:。我还导入了“android.support.v7.app.ActionBarActivity”,但我不确定是否需要所有这些,因为我没有隐式使用操作栏(但我可能会在未来)。



代码文件

build.gradle(模块:应用程序)

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion '21.1.2'

defaultConfig {
applicationId "com.kjackson324.pikcms"
minSdkVersion 10
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
}

dependencies {
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
compile fileTree(include: ['*.jar'], dir: 'libs')
}




AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kjackson324.pikcms" >

<application
android:name=".objects.PikCMS"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/ThemeOverlay.AppCompat"
>
<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

<activity
android:name=".Login"
android:configChanges="keyboardHidden|orientation"
android:label="@string/title_activity_login"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.kjackson324.pikcms.Login" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Navigation"
android:configChanges="keyboardHidden|orientation"
android:label="@string/title_activity_navigation"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.kjackson324.pikcms.Navigation" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".PickHistory"
android:configChanges="keyboardHidden|orientation"
android:label="@string/title_activity_pik_log"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.kjackson324.pikcms.PickHistory" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<!--

...bla, bla, bla...

(much more of the same, no new attributes used)
-->

<activity
android:name=".ConfigLocFields"
android:label="@string/title_activity_config_loc_fields"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.kjackson324.pikcms.Navigation" />
<intent-filter>
<action android:name="com.kjackson324.pikcms.ConfigLocFields" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>




样式.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="simple_center" parent="">
<!--item name="android:layout_gravity">center</item-->
<!--item name="android:gravity">center</item-->
<item name="android:dialogLayout">?android:layout_gravity</item>
</style>

<style name="lblLoggedIn" parent="@android:style/TextAppearance.Small">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:text">@string/sys_logged_in</item>
<item name="android:layout_gravity">top|right</item>
<item name="android:gravity">top|center_vertical</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:paddingLeft">4dp</item>
<item name="android:paddingRight">4dp</item>
</style>

<style name="tvLoggedIn" parent="@android:style/TextAppearance.Small">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:inputType">none</item>
<item name="android:gravity">top</item>
<item name="android:layout_gravity">top|left</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:layout_weight">2</item>
</style>

<style name="layout_master">
<item name="android:orientation">vertical</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!--item name="showDividers">end</item-->
<item name="android:layout_alignParentTop">true</item>
<item name="android:layout_centerHorizontal">true</item>
</style>

<style name="layout_loggedin">
<!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">?android:attr/dividerVertical</item>
<item name="android:gravity">bottom|center</item>
</style>

<style name="layout_bodyscroll">
<item name="android:layout_width">fill_parent</item>
<item name="android:fillViewport">true</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_marginTop">@dimen/activity_bodyscroll_margin</item>
<item name="android:layout_marginBottom">@dimen/activity_bodyscroll_margin</item>
<item name="android:layout_marginLeft">@dimen/activity_bodyscroll_margin</item>
<item name="android:layout_marginRight">@dimen/activity_bodyscroll_margin</item>
</style>

<style name="layout_bodylinear">
<item name="android:orientation">vertical</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>

<style name="layout_btnbar_frame">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">1</item>
</style>

<style name="layout_btnbar_linear" parent="">
<!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">?android:attr/dividerVertical</item>
<item name="android:gravity">bottom|center</item>
<item name="android:layout_gravity">bottom</item>
</style>

<style name="layout_btnbar_btn" parent="">
<!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:textColor">@android:color/primary_text_dark</item>
<item name="android:paddingTop">@dimen/activity_vertical_margin</item>
<item name="android:paddingBottom">@dimen/activity_vertical_margin</item>
</style>

<style name="menu_btn" parent="layout_btnbar_btn">
<item name="android:textAppearance">@android:style/TextAppearance.Large</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>

<style name="hidden">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:editable">false</item>
<item name="android:autoText">false</item>
<item name="android:visibility">invisible</item>
<item name="android:inputType">none</item>
</style>

<style name="field_edittext">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:layout_marginTop">3dp</item>
<item name="android:layout_marginLeft">2dp</item>
<item name="android:layout_marginRight">2dp</item>
<item name="android:layout_column">1</item>
<item name="android:layout_weight">2</item>
<item name="android:textColor">@android:color/primary_text_dark</item>
<item name="android:autoText">true</item>
<item name="android:gravity">bottom</item>
</style>

<style name="tvFieldLabel">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:layout_gravity">top</item>
<item name="android:gravity">top</item>
</style>

<style name="field_edittext_note" parent="field_edittext">
<item name="android:layout_height">125dp</item>
<item name="android:gravity">top</item>
</style>

<style name="tvTableField">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_gravity">bottom|right</item>
<item name="android:layout_weight">0</item>
<item name="android:layout_column">0</item>
<item name="android:textAppearance">@android:style/TextAppearance.Medium</item>
<item name="android:textColor">@android:color/secondary_text_dark</item>
<item name="android:gravity">center_vertical</item>
<item name="android:inputType">none</item>
</style>

<style name="spinTails">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!--item name="android:background">?android:attr/dividerVertical</item-->
<item name="android:padding">8dp</item>
</style>


<style name="tvFieldColPrimary" parent="tvTableField">
<item name="android:textColor">@android:color/primary_text_dark</item>
<item name="android:paddingRight">8dp</item>
</style>

<style name="tvFieldColSecondary" parent="tvFieldColPrimary">
<item name="android:textColor">@android:color/secondary_text_dark</item>
</style>

</resources>




./layout/activity_login.xml (我的第一个 Activity )

<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"
tools:context="com.kjackson324.pikcms.Login"
android:id="@+id/layLogin" >

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">

<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/tvId"
android:visibility="invisible"/>

<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:id="@+id/scrollView4"
android:layout_weight="1">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout13"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/entry_employee"
android:id="@+id/tvName"
/>

<Spinner
android:id="@+id/spinEmployees"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/lbl_password"
android:id="@+id/tvPassword"
android:layout_marginTop="@dimen/activity_vertical_margin"/>

<EditText
android:id="@+id/txtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:ems="10"
android:text="admin"/>

</LinearLayout>
</ScrollView>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/linearLayout14"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom|center"
android:weightSum="2"
android:layout_gravity="bottom">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_begin"
android:id="@+id/btnLogin"
android:background="@android:color/transparent"
android:layout_weight="1"
android:textColor="@android:color/primary_text_dark"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:onClick="onClick"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_clear"
android:id="@+id/button7"
android:background="@android:color/transparent"
android:layout_weight="1"
android:textColor="@android:color/primary_text_dark"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"/>
</LinearLayout>
</FrameLayout>

</LinearLayout>

</RelativeLayout>




设备 |日志

03-03 11:14:37.386    1891-1891/com.kjackson324.pikcms D/AndroidRuntime﹕ Shutting down VM
03-03 11:14:37.386 1891-1891/com.kjackson324.pikcms W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-03 11:14:37.413 1891-1891/com.kjackson324.pikcms E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kjackson324.pikcms/com.kjackson324.pikcms.Login}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
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.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.kjackson324.pikcms.Login.onCreate(Login.java:38)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

您需要在 list 的应用程序标签中替换您的主题:

<application android:theme="@style/Theme.AppCompat">

ThemeOverlay 只能与具有新 actionBarTheme 属性的 actionBar 或工具栏一起使用。更多信息在这里:https://chris.banes.me/2014/11/12/theme-vs-style/

关于android - 如何在整个应用程序范围内使用 Android 的 AppCompat 主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838253/

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