2017-08-07 145 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

如果重建或乾淨的沒有幫助,儘量做到:文件 - 無效緩存/重啓