Disable the buttons on runtime
I'm trying to enable a button when I execute a function but it gives me an FC Error:
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..........................
My code is:
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;
//bt开发者_如何学运维nValidar.setEnabled(false);
}else {
Log.d("PRUEBA","ESTA BIEN");
return true;
//btnValidar.setText("pulsado");
//btnValidar.setEnabled(true);
}
}
Could you help me please?
java.lang.NullPointerException
that mean u forget to supply findViewById(R.id.controlid);
to any control try to find out this in your code may this solves this error Happy Coding :):) :- Pragna
make sure to initialize btnValidar
using findViewById()
before trying to access it.
精彩评论