Android “Cannot resolve symbol” for setOnCheckedChangeListener and buttonView

孤人 提交于 2019-12-25 15:38:26

问题


I'm trying to create a switch which when enabled hides a button called button but it returns an error for SetOnCheckedChangeListener and buttonView which says Cannot resolve symbol

package com.jordthedev.firstapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    int count = 0;


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

    }


    public void switchActivity(View v){
        startActivity(new Intent(MainActivity.this, SecondActivity.class));
    }

    ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                toggle.setVisibility(toggle.INVISIBLE);
            } else {
                toggle.setVisibility(toggle.VISIBLE);
            }
        }
    });

}

回答1:


Maybe you have to place ToggleButton in onCreate() method try above code

package com.jordthedev.firstapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    int count = 0;


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

        final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    toggle.setVisibility(toggle.INVISIBLE);
                } else {
                    toggle.setVisibility(toggle.VISIBLE);
                }
            }
        });
    }


    public void switchActivity(View v){
        startActivity(new Intent(MainActivity.this, SecondActivity.class));
    }



}



回答2:


Replace your OnCheckedChangeListener Code with this

ToggleButton toggle =(ToggleButton) findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    toggle.setVisibility(toggle.INVISIBLE);
                } else {
                    toggle.setVisibility(toggle.VISIBLE);
                }
            }
        });


来源:https://stackoverflow.com/questions/44413251/android-cannot-resolve-symbol-for-setoncheckedchangelistener-and-buttonview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!