Argument this doesn´t work. [required: 'android.content.context'
11,501
Solution 1
Use
getActivity().getApplicationContext()
instead of
this
Solution 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)
Solution 3
Use :
actionBar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
R.color.action_bar));
Author by
Yannick - theSpy_007
Updated on September 07, 2022Comments
-
Yannick - theSpy_007 almost 2 years
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'
-
m02ph3u5 over 8 yearsWhen you're in a fragment you cannot pass
this
for it's a Fragment, not an Activity and thus not a subtype ofContext
. UsegetContext()
instead. -
OneCricketeer over 8 yearsA Fragment is not a Context
-
-
Yannick - theSpy_007 over 8 yearsNo than Android studio say : cannot resolve symbol: 'getActivity()'
-
Yannick - theSpy_007 over 8 yearsSame like above. Android Studio say: Cannot resolve symbol: 'getContext()'
-
m02ph3u5 over 8 yearsWhat's
org.telegram.ui.ActionBar.BaseFragment
? If it's a subclass of androidsFragment
and you haven't messed withgetContext()
orgetActivity()
either should work. Also see stackoverflow.com/questions/8215308/using-context-in-a-fragment