NumberFormatException:无效的int:“HH”

时间:2015-06-23 08:59:18

标签: android android-date

我收到错误Invalid int:“HH”。它只发生在某些设备上。我认为问题在于转换,但为什么它适用于大多数设备?

这是 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)

这是我的代码( 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);

                 }

4 个答案:

答案 0 :(得分:3)

请使用此 -

String cdate = (String) DateFormat.format("hh:mm", c.getTime());

24小时格式请使用此< - strong>

String cdate = (String) DateFormat.format("H:m", c.getTime());

而不是 -

String cdate = (String) DateFormat.format("HH:mm", c.getTime());

答案 1 :(得分:0)

我认为&#34; HH&#34;应该是小后者,如

String cdate = (String) DateFormat.format("hh:mm", c.getTime());      

答案 2 :(得分:0)

    Fragmenet

private static final int year = 0;
    private static final int monthOfYear = 0;
    private static final int dayOfMonth = 0;


    @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
            time=(Button) getActivity().findViewById(R.id.btnsettime);
            date=(Button) getActivity().findViewById(R.id.btnsetdate);

            time.setOnClickListener(this);
            date.setOnClickListener(this);

        }
    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v==time)
            {
                Calendar cal=Calendar.getInstance();
                int hour=cal.get(Calendar.HOUR);
                int minute=cal.get(Calendar.MINUTE);

                TimePickerDialog tpd=new TimePickerDialog(getActivity(), new OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        // TODO Auto-generated method stub

                        tvtime.setText(hourOfDay+":"+minute);
                        calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calender.set(Calendar.MINUTE, minute);

                    }

                }, hour, minute, true);
                tpd.show();


            }
            if(v==date)
            {

                Calendar cal=Calendar.getInstance();
                int day=cal.get(Calendar.DAY_OF_MONTH);
                int month=cal.get(Calendar.MONTH);
                int year=cal.get(Calendar.YEAR);

                DatePickerDialog dpg=new DatePickerDialog(getActivity(), new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                            int dayOfMonth) {
                        // TODO Auto-generated method stub

                        tvdate.setText(dayOfMonth+":"+(monthOfYear+1)+":"+year);
                        calender.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        calender.set(Calendar.MONTH, monthOfYear);
                        calender.set(Calendar.YEAR, year);
                    }
                }, year, month, day);
dpg.show();

            }
    }

答案 3 :(得分:0)

根据doc,在API级别17之前,仅支持adEhkMmszy。请注意,为了向后兼容,这个类错误地实现了k,好像它是H一样。

改变&#39; HH&#39;如果您需要24小时格式,请选择“kk&#39;”。提醒一下,&#39; H&#39;是指小时(0-23),&#39; k&#39;是指一天中的小时(1-24)