I get the error Invalid int:"HH". It only occurs on some devices. I think the problem is in the conversion , but why does it work on most devices ?
This is the logcat:
java.lang.NumberFormatException: Invalid int: "HH"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:375)
at java.lang.Integer.parseInt(Integer.java:366)
at java.lang.Integer.parseInt(Integer.java:332)
at com.lp.lemiediete.InserisciOra$4.onClick(InserisciOra.java:173)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:148)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
and this is my code(InserisciOra.class):
breakfast = (EditText) view.findViewById(R.id.timebreak);
breakfast.setFocusable(false);
breakfast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int hour = 7;
int minute = 0;
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
Calendar c =Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,selectedHour);
c.set(Calendar.MINUTE,selectedMinute);
String cdate = (String) DateFormat.format("HH:mm", c.getTime());
breakfast.setText(cdate);
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("breakfast", cdate).commit();
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Inserisci l'orario di colazione ");
mTimePicker.show();
}
});
btniniziaora = (Button) view.findViewById(R.id.btn_iniziaora);
btniniziaora.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String breakfast1=breakfast.getText().toString();
String lunch1=lunch.getText().toString();
String dinner1=dinner.getText().toString();
if(breakfast1.equals("") || lunch1.equals("") || dinner1.equals("")){
Toast.makeText(getActivity(), "Completa tutti i campi!", Toast.LENGTH_LONG).show();
return;
}
else{
d=new Database(getActivity());
String dataString=d.checkDataString();
//COLAZIONE
int duepunti = breakfast1.indexOf(":");
String hourColazione = breakfast1.substring(0,duepunti);
int intHourColazione=Integer.parseInt(hourColazione);
String minuteColazione = breakfast1.substring(duepunti + 1);
int intMinuteColazione=Integer.parseInt(minuteColazione);
int start = dataString.indexOf("/");
String dayString = dataString.substring(0,start);
String monthString = dataString.substring(start + 1,start+3);
String yearString = dataString.substring(start+4);
int day = Integer.parseInt(dayString);
int month = Integer.parseInt(monthString);
int year = Integer.parseInt(yearString);
Calendar calNow = Calendar.getInstance();
Calendar c = (Calendar) calNow.clone();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, intHourColazione);
c.set(Calendar.MINUTE, intMinuteColazione);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
if(c.after(System.currentTimeMillis())){
c.add(Calendar.DAY_OF_MONTH, 1);
}
//PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("keyid", "0").commit();
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
if(PendingIntent.getBroadcast(getActivity(), 0,
myIntent,
PendingIntent.FLAG_NO_CREATE) == null){
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Please use this -
And for 24 Hour format please use this -
instead of this-