What I want is to have an EditText that when pressed will display a dialog in which you choose the hour and the minutes. The dialog box comes up, but when I click on the accept option I get the classic "Unfortunately the app stopped" ...
This is what I have done so far ...
public class Activity_llamadas extends Activity {
Button boton;
EditText editText;
TextView textView;
int h=0;
int m=0;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_llamadas);
textView=(TextView)findViewById(R.id.textViewHora);
final TimePicker timePicker= (TimePicker)findViewById(R.id.timePickerInicio);
boton=(Button)findViewById(R.id.buttonActHora);
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hora= timePicker.getCurrentHour();
int min=timePicker.getCurrentMinute();textView.setText(String.valueOf(hora)+": "+String.valueOf(min)); //aqui me funciona bien
}
});
editText=(EditText)findViewById(R.id.getHora);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTime();
}
});
} public void showTime(){
AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_set_time,null))
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) { // EL PROBLEMA ESTA AQUI
final TimePicker timePicker= (TimePicker)findViewById(R.id.timePicker);
TextView textView=(TextView)findViewById(R.id.textViewHora);
h=timePicker.getCurrentHour();
m=timePicker.getCurrentMinute();
textView.setText(String.valueOf(h)+": "+String.valueOf(m)); //textView donde pongo la hora
}
});
builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { //esto tambien funciona bien
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"Por favor, seleccione una hora",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert=builder.create();
alert.show(); }}'
This is what the logCat shows me with the error filter:
02-18 17:59:29.750 17266-17266/com.example.andry.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andry.myapplication, PID: 17266
java.lang.NullPointerException
at com.example.andry.myapplication.Activity_llamadas$3.onClick(Activity_llamadas.java:70)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
at dalvik.system.NativeStart.main(Native Method)
Line 70 is this: h = timePicker.getCurrentHour ();
THANK YOU IN ADVANCE