为什么Android Studio不会找到我的资源?

luk*_*rop 21 android gradle android-studio

Android Studio代码检查似乎找不到任何项目特定的资源.

例如我的styles.xml

<resources>
  <style name="AppBaseTheme" parent="Holo.Theme.Light.DarkActionBar">
  </style>

  <!-- Application theme. -->
  <style name="AppTheme" parent="AppBaseTheme">
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

标记第二个AppBaseTheme红色并说:"无法解析符号'AppBaseTheme'".

该应用程序编译和运行没有问题,但我没有任何代码完成资源.检查似乎找到了ActionBarSherlock资源和HoloEverywhere资源(例如Holo.Theme.Light.DarkActionBar,自动完成也可以).

app(sub)项目的build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.1'
    }
}
apply plugin: 'android'

dependencies {
    //compile files('libs/android-support-v4.jar')
    compile project(':abs-library')
    compile project(':he-library')
    compile project(':he-addons:slider')
    compile project(':he-addons:preferences')
    // using a custom repo here
    compile 'com.google.android:support-v4:r13'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 17
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            // abs-res, he-res are symlinks to ../abs-library/res
            // and ../he-library/res 
            res.srcDirs = ['src/main/res', 'abs-res', 'he-res']
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

bco*_*rso 47

我要尝试的第一件事是__CODE__.

这解决了我在Android Studio中遇到的所有资源问题.

  • 无需重启,您只需执行“ Invalidate”然后关闭然后重新打开项目即可。这比重新启动要快一些。 (2认同)

Zin*_*inc 7

尝试使用工具栏中的"使用Gradle文件同步项目"按钮(带有蓝色箭头的绿色圆圈),它对我有用

您还可以强制同步更改您的gradle中的某些内容(同步按钮将弹出,您应该单击它),然后还原更改并再次单击"同步"


Yoa*_*uet 4

你的样式定义有问题,你必须调用用“@”定义的样式:

<resources>
  <style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
  </style>

  <!-- Application theme. -->
  <style name="AppTheme" parent="@style/AppBaseTheme">
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

@style/AppBaseTheme这里的意思是系统将调用AppBaseTheme你的styles.xml文件中命名的样式。