Android: When using setOnCheckedChangeListener on switches I get 'Cannot Resolve Symbol Error'
Asked Answered
S

3

5
package com.example.koustav.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    Switch swi = (Switch) findViewById(R.id.swch);
    swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) //Line A
        {

        }
    });

    public void onClick(View view)
    {
        boolean isOn = ((Switch) view).isChecked();

        if (isOn)
        {
            ((TextView) findViewById(R.id.largey)).setTextColor(getResources().getColor(R.color.black));
        }
        else
        {
            ((TextView) findViewById(R.id.largey)).setTextColor(getResources().getColor(R.color.white));
        }

    }

}

I am using Android Studio for this project. I have searched the interwebs for a solution extensibly but could not find one.

I have been trying to use setOnCheckedChangeListener for a Switch object. However no matter what I try, I always get a Cannot Resolve Symbol 'setOnCheckedChangeListener' error. Also, at the line A, the object buttonView is underlined red giving the same error Cannot Resolve Symbol 'buttonView'.

I have imported the necessary (and recommended as answers for other people) classes. I am working with APIs for android 4.0 and above. I basically want the onClick method to be re-implemented via the listener because the listener works with both clicks and swipes whereas onClick only works with clicks and not swipes.

Stapes answered 24/4, 2015 at 10:23 Comment(0)
B
8

You have to initialize Switch inside onCreate()

Belayneh answered 24/4, 2015 at 10:31 Comment(1)
and put the swi.setOnCheckedChangeListener() and following lines in there as well, up to the semicolon :)Earthenware
U
6

move

  Switch swi = (Switch) findViewById(R.id.swch);
  swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) //Line A
        {

        }
    });

in onCreate. You can't call findViewById, before the activity is created, and swi.setOnCheckedChangeListener(..); is not a valid statement

Uella answered 24/4, 2015 at 10:33 Comment(0)
E
0

Replace the below code

        switchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SwitchButton switchButton = (SwitchButton)view;

by

        switchButton.setOnCheckedChangeListener( new SwitchButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SwitchButton switchButton = (SwitchButton) buttonView;

it will work !

Estus answered 14/2, 2017 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.