Argument this doesn´t work. [required: 'android.content.context'

泪湿孤枕 提交于 2019-12-24 12:06:03

问题


I use this:

actionBar.setBackgroundColor(ContextCompat.getColor(this, R.color.action_bar));

To use colors from the color.xml .

It works great, but in some Codes it says:

Wrong 1st argument type. Found: 'org.telegram.ui.ActionBar.BaseFragment', required: 'android.content.Context'

But I import android.content.Context:

import android.content.Context;

I tried to use instead of 'this':

actionBar.setBackgroundColor(ContextCompat.getColor(context, R.color.action_bar));

But than Android Studio say :

Cannot resolve symbol 'context'

回答1:


Use

getActivity().getApplicationContext()

instead of

this



回答2:


Fragment is not a subtype of Context.

When inside of a fragment use this:

ContextCompat.getColor(getContext(), R.color.action_bar)

When inside an activity you can use:

ContextCompat.getColor(this, R.color.action_bar)



回答3:


Use :

actionBar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), 

R.color.action_bar));


来源:https://stackoverflow.com/questions/35816602/argument-this-doesn%c2%b4t-work-required-android-content-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!