android:theme="@style/apptheme",android studio - setTheme(R.style.AppTheme) not working when creatin...

I display a splash screen on my Android app by using a theme for loading. In the MainActivity I change the theme using setTheme. It works when I build the app from Android Studio on the emulator (it compiles and runs). When I try to build an APK using Build->Build APKs, I get an error.

E:\AndroidStudioProjects\gb\feature\src\main\java\com\zwsi\gb\feature\MainActivity.kt: (25, 20): Unresolved reference: style

What is different when building APK vs. building an app?

AndroidManifest.xml

package="com.perrochon.gb">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme.Launcher">

android:name="aia-compat-api-min-version"

android:value="1"/>

styles.xml

@drawable/app_background

@drawable/launch_background

MainActivity.kt

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

setTheme(R.style.AppTheme) // TODO switch back from the Launcher Theme, but this won't compile to APK

// setTheme(R.style.AppTheme) works in Android Studio -> Emulator, but not when building APKs. Error is

// E:\AndroidStudioProjects\gb\feature\src\main\java\com\zwsi\gb\feature\MainActivity.kt: (25, 20): Unresolved reference: style

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Edit: Adding build gradle files. I haven't really done anything with the gradel build files, they are the way Android Studio set them up.

build.gradle from gb

buildscript {

ext.kotlin_version = '1.2.71'

repositories {

google()

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:3.2.1'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

google()

jcenter()

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

build.gradle from gb\feature

apply plugin: 'com.android.feature'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {

compileSdkVersion 28

defaultConfig {

minSdkVersion 15

targetSdkVersion 28

versionCode 1

versionName "1.0"

testInstrumentationRunner

"android.support.test.runner.AndroidJUnitRunner"

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation project(':base')

implementation project(':gblib')

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

build.gradle from gb\base

apply plugin: 'com.android.feature'

android {

compileSdkVersion 28

baseFeature true

defaultConfig {

minSdkVersion 15

targetSdkVersion 28

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

api 'com.android.support:appcompat-v7:28.0.0'

api 'com.android.support.constraint:constraint-layout:1.1.3'

api 'com.android.support:support-v4:28.0.0'

application project(':app')

feature project(':feature')

}

build.gradle from gb\app

apply plugin: 'com.android.application'

android {

compileSdkVersion 28

defaultConfig {

applicationId "com.zwsi.gb.app"

minSdkVersion 15

targetSdkVersion 28

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

implementation project(':feature')

implementation project(':base')

implementation project(':gblib')

}

I also have gblib directory with a library of non-Android code. build.gradle from gb\lib

plugins {

id 'org.jetbrains.kotlin.jvm'

}

apply plugin: 'java-library'

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

testImplementation 'junit:junit:4.12'

}

sourceCompatibility = "6"

targetCompatibility = "6"

repositories {

mavenCentral()

}

compileKotlin {

kotlinOptions {

jvmTarget = "1.8"

}

}

compileTestKotlin {

kotlinOptions {

jvmTarget = "1.8"

}

}

<think>好的,我现在需要解决用户在AndroidManifest.xml文件中遇到的意外文本错误,具体错误是“unexpected text android:label string app_name MainActivity android theme style Theme Kolu”。用户提供的引用内容涉及AndroidManifest.xml的结构、属性使用以及编译安装时的常见错误。首先,我需要分析这个错误的原因。 根据用户的描述,错误信息中提到了“unexpected text”,这通常表示XML文件中有语法错误,比如标签未正确闭合、属性值缺少引号或格式错误,或者存在非法字符。用户提供的引用[1]、[2]、[3]中提到了android:label、tools:replace、android:exported等属性的使用,这些可能相关。 首先,检查用户提供的错误位置:“android:label string app_name MainActivity android theme style Theme Kolu”。这里可能存在多个问题: 1. **属性值未用引号包裹**:比如`android:label=@string/app_name`如果漏掉引号,会变成`android:label=@string/app_name...`,导致解析错误。 2. **标签结构混乱**:可能在Activity或Service标签中属性顺序混乱,或者标签未正确闭合。 3. **非法字符或格式错误**:例如在属性值中使用了空格但未用引号包裹,或者符号错误。 接下来,参考用户提供的引用内容。引用[1]中的示例显示,android:label属性正确使用了`@string/app_name`,并且用引号包裹。引用[2]提到添加android:exported属性时需要注意格式,确保属性值正确。引用[3]中的错误是由于进程名设置导致的,但用户当前的问题更多是语法错误。 可能的解决步骤: 1. **检查属性语法**:确保所有属性如android:label、android:theme等的值都被正确引号包裹,例如`android:label="@string/app_name"`。 2. **检查标签闭合**:确认所有标签如<activity>、<application>等正确闭合,没有遗漏结束标签或斜杠。 3. **检查特殊字符**:确保属性值中没有非法字符,比如在字符串资源中使用单引号或特殊符号时需要转义。 4. **检查XML结构**:确保所有元素正确嵌套,没有交叉或未正确结束的情况。 可能用户提供的错误行中,属性之间缺少空格或引号,导致解析器读到意外文本。例如,可能写成: `<activity android:name=".MainActivity" android:label=@string/app_name android:theme="@style/Theme.Kolu"/>` 这里缺少引号,应该改为: `<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/Theme.Kolu" />` 此外,用户提到的tools:replace可能与其他合并清单时的冲突有关,但当前错误更可能是语法问题。需要逐步检查AndroidManifest.xml的结构,特别是报错行附近的代码,使用Android Studio的XML验证工具来定位错误。 最后,建议用户使用IDE的提示功能,如Android Studio会标记语法错误,帮助快速定位问题。同时,检查所有引用的资源是否存在,如@string/app_name和@style/Theme.Kolu是否正确定义。</think>要解决AndroidManifest.xml文件中因意外文本导致的错误`unexpected text android:label string app_name MainActivity android theme style Theme Kolu`,请按照以下步骤排查: --- ### 1. **检查属性值的引号完整性** 所有XML属性值必须用**双引号包裹**,例如: ```xml <!-- 错误写法 --> android:label=@string/app_name <!-- 正确写法 --> android:label="@string/app_name" ``` 如果缺少引号,XML解析器会将其后的文本视为非法内容,导致`unexpected text`错误[^1][^3]。 --- ### 2. **验证标签结构** 确保`<activity>`或`<application>`等标签的**属性顺序和闭合符号**正确: ```xml <!-- 示例:MainActivity标签 --> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/Theme.Kolu" <!-- 注意结尾的闭合符号 --> android:exported="true"> <!-- 若需兼容Android 13 --> </activity> ``` - 缺少闭合符号`/>`或`>`会导致后续文本被误读。 - 确保没有交叉嵌套的标签。 --- ### 3. **检查资源引用是否存在** 确认`@string/app_name`和`@style/Theme.Kolu`**是否在项目中正确定义**: - 在`res/values/strings.xml`中检查`app_name`字符串。 - 在`res/values/styles.xml`中检查`Theme.Kolu`样式。 --- ### 4. **排查合并清单冲突** 如果使用了`tools:replace`(如引用[1]),确保它仅用于覆盖冲突属性: ```xml <application tools:replace="android:label" android:label="@string/app_name"> ``` - 避免在同一个标签中重复定义属性。 --- ### 5. **使用Android Studio验证** 通过IDE的**Lint工具**自动检测错误: 1. 打开`AndroidManifest.xml`。 2. 点击右上角**“Inspect Code”**,查看错误提示。 3. 根据提示修正语法或资源引用问题。 --- ### 6. 完整示例修正 错误代码可能类似于: ```xml <activity android:name=".MainActivity" android:label=@string/app_name android:theme=@style/Theme.Kolu/> ``` 修正后: ```xml <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/Theme.Kolu" android:exported="true" /> <!-- 兼容Android 13需添加 --> ``` --- ### 常见错误原因总结 | 错误类型 | 示例 | 解决方案 | |------------------------|----------------------|------------------------------| | 属性值缺少引号 | `android:label=...` | 添加双引号`android:label="..."` | | 标签未闭合 | `<activity ...>` | 添加闭合符号`/>`或`</activity>` | | 资源未定义 | `@style/Theme.Kolu` | 检查`styles.xml`中的定义 | | 属性重复或冲突 | 多次定义`android:label` | 使用`tools:replace`合并[^1] | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值