在运行时禁用按钮

时间:2011-06-22 11:37:29

标签: android

我正在尝试在执行某个功能时启用一个按钮,但它给了我一个FC错误:

06-22 11:26:25.050: WARN/dalvikvm(688): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-22 11:26:25.089: ERROR/AndroidRuntime(688): FATAL EXCEPTION: main
06-22 11:26:25.089: ERROR/AndroidRuntime(688): java.lang.NullPointerException
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at com.es.inicio$1.onDateChanged(inicio.java:58)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.widget.DatePicker.notifyDateChanged(DatePicker.java:397)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.widget.DatePicker.access$100(DatePicker.java:46)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.widget.DatePicker$1.onChanged(DatePicker.java:100)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.widget.NumberPicker.notifyChange(NumberPicker.java:340)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.widget.NumberPicker.changeCurrent(NumberPicker.java:330)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.widget.NumberPicker$3.onClick(NumberPicker.java:153)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.view.View.performClick(View.java:2485)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.view.View$PerformClick.run(View.java:9080)
06-22 11:26:25.089: ERROR/AndroidRuntime(688):     at android.os.Handler.handleCallback(Handler.java:587)
l..........................

我的代码是:

    DatePicker.OnDateChangedListener listenerCambiado = new DatePicker.OnDateChangedListener() {

        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
               mYear = year;
               mMonth = monthOfYear;
               mDay = dayOfMonth;
               Log.d("PRUEBA","ENTRAMOS EN LISTENER DEL DATEPCKER");
               //comprobarFecha(mYear,mMonth,mDay);
               if (comprobarFechas(mYear,mMonth,mDay)){
                    btnValidar.setEnabled(true);               
               }else{

                    btnValidar.setEnabled(false);

               }
        }
    };

        The function is:

public boolean comprobarFechas (int ano, int mes, int dia){
    Calendar c = Calendar.getInstance();
    if (ano > c.get(Calendar.YEAR)){
        Toast.makeText( getApplicationContext(),"AÑO INCORRECTO", Toast.LENGTH_LONG).show();
        Log.d("PRUEBA","AÑO INCORRECTO");
        return false;
        //btnValidar.setEnabled(false);
    }else if (mes > c.get(Calendar.MONTH) && ano == c.get(Calendar.YEAR)){          
        Toast.makeText( getApplicationContext(),"MES INCORRECTO", Toast.LENGTH_LONG).show();
        Log.d("PRUEBA","MES INCORRECTO");
        return false;
        //btnValidar.setEnabled(false);
    }else {
        Log.d("PRUEBA","ESTA BIEN");
        return true;
        //btnValidar.setText("pulsado");
        //btnValidar.setEnabled(true);

    }
}

你能帮我吗?

2 个答案:

答案 0 :(得分:0)

java.lang.NullPointerException这意味着你忘记向任何控件提供findViewById(R.id.controlid);试图在你的代码中找到这个可能这解决了这个错误Happy Coding :) :): - Pragna

答案 1 :(得分:0)

确保在尝试访问之前使用btnValidar初始化findViewById()