项目创建完成后,我正在使用SDK 28创建一个新的android项目,我在MainActivity中遇到错误。
我尝试了一些方法:
清理项目
清理Android Studio缓存
清理Gradle缓存
重新安装的IDE
文件->使缓存/重启无效
删除.idea文件夹
重新启动Android Studio。
Android Studio关于:
Android Studio 3.3.2
Build #AI-182.5107.16.33.5314842, built on February 15, 2019
JRE: 1.8.0_152-release-1248-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.israteneda.myapplicationtest2"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
}
MainActivity.java
package com.israteneda.myapplicationtest2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
显示的错误是:
Android resource linking failed
error: resource style/Theme.AppCompat.Light.DarkActionBar (aka com.israteneda.horariu:style/Theme.AppCompat.Light.DarkActionBar) not found.
D:\israt\android_apps\HorariU\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:173: error: style attribute 'attr/colorPrimary (aka com.israteneda.horariu:attr/colorPrimary)' not found.
D:\israt\android_apps\HorariU\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:174: error: style attribute 'attr/colorPrimaryDark (aka com.israteneda.horariu:attr/colorPrimaryDark)' not found.
D:\israt\android_apps\HorariU\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:175: error: style attribute 'attr/colorAccent (aka com.israteneda.horariu:attr/colorAccent)' not found.
error: failed linking references.
当我将SDK更改为27时,MainActivity起作用。
build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.israteneda.horariu"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
}
MainActivity.java
package com.israteneda.myapplicationtest2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
还可以使用androidx库。
build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.israteneda.myapplicationtest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
MainACtivity.java
package com.israteneda.myapplicationtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我希望创建一个没有问题的android项目,但实际输出是MainActivity上的AppCompatActivity错误。为什么会发生此问题?
答案 0 :(得分:0)
1。转到您的项目目录(或项目视图)
2。查找并打开.idea目录
3。删除缓存和库目录
4。使缓存无效/重新启动
答案 1 :(得分:0)
该问题已通过更新新版本的android studio 3.4得以解决,而无需导入previus版本的设置。
答案 2 :(得分:0)
我遇到了同样的问题,并通过更新Android Studio解决了该问题