2015-12-31 65 views
1

代碼:安卓ContextCompat.getColor無法解決方法

backgroundColor = typedArray.getColor(R.styleable.TapBarMenu_tbm_backgroundColor, 
ContextCompat.getColor(getContext(), R.color.red)); 

是在編輯器中ContextCompat.getColorgetcolor紅 - 「無法解析的方法」。該應用程序編譯並運行。在調試過程中,當我在評估表達式ContextCompat.getColor(getContext(), R.color.red)之前,在執行上述錯誤代碼之前,嘗試在無準備的類「Landroid/support/v4/content/ContextCompat;'中執行代碼」,如果我跳過該行,表達工作正常。

問題:如何擺脫Android Studio編輯器中顯示的錯誤,因爲它很煩人。另外,這個錯誤是否意味着什麼?

+0

「無法解析方法」 - 確保您在項目中使用最新版本的Android支持庫。 – CommonsWare

回答

0

評論import android.support.v4.content.ContextCompat;和重建擺脫了Android Studio編輯器中的錯誤,重新添加import android.support.v4.content.ContextCompat;

1
dependencies { 
    // other stuff here 
    compile 'com.android.support:support-v4:23.0.0' 
} 

添加此依賴關係後,它將正常工作。由於ContextCompat是在support-v4中添加的,因此必須在您的項目中使用它。