【问题标题】:AndroidStudio onCheackedChangeListenerAndroid Studio onCheckedChangeListener
【发布时间】:2019-03-10 07:15:33
【问题描述】:

我想学习如何使用监听器onCheckedChangeListener,但是每次我实现它时,应用程序都会关闭。我知道可以用onCLick 完成,但我需要知道如何使用它:

package com.example.tonij.colores;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    CheckBox rojo,verde,azul;
    Button ponerColor, borrar;
TextView texto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        rojo= (CheckBox) findViewById(R.id.botonRojo);
        verde= (CheckBox) findViewById(R.id.botonVerde);
        azul= (CheckBox) findViewById(R.id.botonAzul);

        rojo.setOnCheckedChangeListener(
                new CheckBox.OnCheckedChangeListener(){
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked==true){
                            texto.setText("enga");
                        }
                    }
                }
        );

        ponerColor= (Button) findViewById(R.id.ponerColor);
        borrar= (Button) findViewById(R.id.borrar);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

【问题讨论】:

  • 不确定你想要什么,但你还没有初始化你的textTo TextView,它肯定会崩溃。还将if(isChecked==true) 更改为if(isChecked)。好难受
  • 好的,谢谢,但我认为这不是问题,当我使用代码时它返回给我一个空点异常 --> 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)'

标签: java android checkbox


【解决方案1】:

替代方法 导航到“开发人员选项”,确保启用“USB调试”,然后启用“错误报告快捷方式”。通过按住电源按钮并选择“获取错误报告”选项来捕获错误报告。

【讨论】:

    【解决方案2】:

    您在 onCreate() 方法之前声明这些小部件可能会导致问题。但是,您也从未初始化过TextView (texto)。

    所以试试这个:

    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
            final TextView texto = (TextView) findViewById(R.id.yourTextviewid);
            ponerColor= (Button) findViewById(R.id.ponerColor);
            borrar= (Button) findViewById(R.id.borrar);
            rojo= (CheckBox) findViewById(R.id.botonRojo);
            verde= (CheckBox) findViewById(R.id.botonVerde);
            azul= (CheckBox) findViewById(R.id.botonAzul);
    
            rojo.setOnCheckedChangeListener(
                    new CheckBox.OnCheckedChangeListener(){
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if(isChecked){
                                texto.setText("enga");
                            }
                        }
                    }
            );
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多