Search code examples
javaandroidandroid-studioandroid-spinnerandroid-switch

"wrong first argument type. Found: 'android.widget.Compoundbutton.oncheckedchangelistener', required: 'android.content.context'"


this code:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Hours_array, R.layout.MainActivity);       
adapter.setDropDownViewResource(R.layout.MainActivity);  
Hrs.setAdapter(adapter);

the "this" in the first line of code throws this error:

wrong first argument type. Found: 'android.widget.Compoundbutton.oncheckedchangelistener', required: 'android.content.context

I am trying to use a switch to change a spinner's entry that includes hours, from regular time, to military time.

If you can help please do. thank you very much!


Solution

  • When you refer this inside oncheckedchangelistener, it will refer to oncheckedchangelistener object and not MainActivity.

    Instead of

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Hours_array, R.layout.MainActivity); 
    

    Use

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(MainActivity.this, R.array.Hours_array, R.layout.MainActivity);