Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 447
Yannick - theSpy_007 Avatar asked Jul 30 '26 15:07

Yannick - theSpy_007


2 Answers

Use

getActivity().getApplicationContext()

instead of

this
like image 181
user3673952 Avatar answered Aug 02 '26 05:08

user3673952


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)
like image 30
m02ph3u5 Avatar answered Aug 02 '26 04:08

m02ph3u5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!