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));
Share:
11,501
Yannick - theSpy_007
Author by

Yannick - theSpy_007

Updated on September 07, 2022

Comments

  • Yannick - theSpy_007
    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
      m02ph3u5 over 8 years
      When you're in a fragment you cannot pass this for it's a Fragment, not an Activity and thus not a subtype of Context. Use getContext() instead.
    • OneCricketeer
      OneCricketeer over 8 years
      A Fragment is not a Context
  • Yannick - theSpy_007
    Yannick - theSpy_007 over 8 years
    No than Android studio say : cannot resolve symbol: 'getActivity()'
  • Yannick - theSpy_007
    Yannick - theSpy_007 over 8 years
    Same like above. Android Studio say: Cannot resolve symbol: 'getContext()'
  • m02ph3u5
    m02ph3u5 over 8 years
    What's org.telegram.ui.ActionBar.BaseFragment? If it's a subclass of androids Fragment and you haven't messed with getContext() or getActivity() either should work. Also see stackoverflow.com/questions/8215308/using-context-in-a-fragm‌​ent