2017-08-07 154 views
1

我还没有找到正确的答案。这是我与Android Studio的第一个项目,我几乎失去了耐心:)。 Theme.AppCompat.Light.DarkActionBar标记为红色。Android Studio无法解析符号Theme.AppCompat.Light.DarkActionBar /无法解析“R”

这是我的styles.xml:

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
</style> 

</resources> 

这是我的build.gradle

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 25 
buildToolsVersion "25.0.3" 
defaultConfig { 
    applicationId "helloworld.spsa3.helloworldproject" 
    minSdkVersion 10 
    targetSdkVersion 25 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 


dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
compile 'com.android.support:appcompat-v7:25.1.0' 
testCompile 'junit:junit:4.12' 
} 

最后,这是我的第二个问题,我敢肯定,他们是相关的。 “R”也无法解决。 “R”也用红色标出。

因此,这里是我的MainActivityFragment()类:

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class MainActivityFragment extends Fragment{ 

public MainActivityFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 

Bundle savedInstanceState) { 

    return inflater.inflate(R.layout.fragment_main, container, false); 

} 
} 

任何想法?

+0

这意味着在你的布局文件或可绘制一些编译问题。基本上res文件夹中的任何东西都可能导致R无法编译 –

+0

尝试构建 - >清理项目 –

+0

@kapsym所以你有一个想法如何解决这个问题..你有过同样的问题吗? – Sarina

回答

0

R.java是生成的文件。如果你得到象这样的错误

enter image description here

这是因为R.java未能产生。这可能是由错误的文件名或资源文件中的错误内容引起的。

在Android Studio中查找“消息”视图(或通过菜单View -> Tool Windows -> Messages打开)。当您彻底重建项目时,应该会看到与为什么R.java无法生成相关的错误。对于我上面的例子中,该错误信息是:

/StackOverflow/app/src/main/res/drawable/illegal-name.xml

Error: '-' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

所以你可以看到我的illegal-name.xml资源不能有连字符,并且可能应该改为legal_name.xml

或者,也许我搞砸了我的XML标签的地方。也许在我的字符串资源之一,我没有正确关闭标签:

<string name="label_register">REGISTER<string> 

现在的“消息”视图显示了我这个错误:

/StackOverflow/app/src/main/res/values/strings.xml

Error: The element type "string" must be terminated by the matching end-tag " </string> ".

按照你的“消息”的错误观点找到问题。

+0

嗨Ben谢谢。所以我做了gradle同步,并在这之后做了一些错误,如消息所示:“无法解析:javax.inject:javax.inject:1”。有错误是所有的文件“build.gradle”。那么你是否知道我的问题有其他想法或解决方案? – Sarina

+0

这也显示在消息视图中“错误:配置项目出现问题':app' >无法解析配置的所有依赖关系::app:_debugUnitTestApkCopy' >无法解析junit:junit:4.12 需求人: 项目:app >无法解析junit:junit:4.12。 >无法获取资源'https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.pom'。 >无法GET'https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.pom'。 >连接到jcenter.bintray.com:443 – Sarina

+0

这是最后一行,它没有不符合其他评论:>连接到jcenter.bintray.com:443[jcenter.bintray.com/5.153.35.248]失败:连接超时:连接 – Sarina

0

如果重建或干净的没有帮助,尽量做到:文件 - 无效缓存/重启