Home » Android » Exception error

Exception error

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have developed an app which turns the device in silent mode and back to normal on given times set by the user. User will enter the name of the event then input start and end time and then click the create button to create the event. I ran the app and user selects the start time at this spot my app crashes and throws an exception which I am unable to understand. I am posting the related code below and the exception error.

snapshot of the exception

Exception error

10-22 19:13:52.538 13334-13334/applications.recyclerview1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: applications.recyclerview1, PID: 13334
                                                                        java.lang.ArrayIndexOutOfBoundsException: length=17; index=19
                                                                            at java.util.Calendar.get(Calendar.java:903)
                                                                            at applications.recyclerview1.MainActivity$3.onTimeSet(MainActivity.java:95)
                                                                            at android.app.TimePickerDialog.onClick(TimePickerDialog.java:133)
                                                                            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5669)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

10-22 19:13:52.588 13334-13334/applications.recyclerview1 I/Process: Sending signal. PID: 13334 SIG: 9
MainActivity.java

public void start(View view)
{
    // Get Current Time
    Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

   final Calendar newobj=Calendar.getInstance();

    // Launch Time Picker Dialog
    TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {


                    start.setText(hourOfDay + ":" + minute);
                    newobj.get(hourOfDay);
                    newobj.get(minute);

                    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);

                    //create a pending intent to be called at midnight
                    Toast.makeText(MainActivity.this, "Pending Intent started", Toast.LENGTH_LONG).show();
                    PendingIntent midnightPI = PendingIntent.getService(MainActivity.this, 0, new Intent("applications.recyclerview1.SilenceBroadCastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);

                    //schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day

                    am.setRepeating(AlarmManager.RTC_WAKEUP, newobj.getTimeInMillis(), AlarmManager.INTERVAL_DAY, midnightPI);

                }
            }, mHour, mMinute, false);
    timePickerDialog.show();
}

public void end(View view)
{
    // Get Current Time
    Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

    final Calendar newobj2=Calendar.getInstance();

    // Launch Time Picker Dialog
    TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,
                                      int minute) {

                    end.setText(hourOfDay + ":" + minute);
                    newobj2.get(hourOfDay);
                    newobj2.get(minute);


                    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);

                    PendingIntent sixPI = PendingIntent.getService(MainActivity.this, 0, new Intent("applications.recyclerview1.UnsilenceBroadcastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);

                    //schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day

                    am.setRepeating(AlarmManager.RTC_WAKEUP, newobj2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sixPI);

                }
            }, mHour, mMinute, false);
    timePickerDialog.show();
}

AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="applications.recyclerview1">

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".View_Foods"></activity>
    <receiver android:name=".SilenceBroadCastReceiver">

        <intent-filter>

            <action android:name="applications.recyclerview1.SilenceBroadCastReceiver" >

            </action>

        </intent-filter>

    </receiver>

    <receiver android:name=".UnsilenceBroadcastReceiver">

        <intent-filter>

            <action android:name="applications.recyclerview1.UnsilenceBroadcastReceiver" >

            </action>

        </intent-filter>

    </receiver>
</application>

SilenceBroadCastReceiver.java

public class SilenceBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    AudioManager audio=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}}

UnsilentBroadCastReceiver.java

public class UnsilenceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    AudioManager audio=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}}
Answers:

Try following this way for your required process:

    Calendar calender = Calendar.getInstance();
    myTimePicker = new TimePickerDialog(this, new OnTimeSetListener() {

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {


            Calendar newTime = Calendar.getInstance();

            //newTime.set(hourOfDay, minute); // remove this line

            //Add these two line
            newTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            newTime.set(Calendar.MINUTE, minute);

            GotTime = GotDate + " At " + String.valueOf(hourOfDay) + " :" + String.valueOf(minute + 1);
            time.setText(GotTime);
        }
    }, calender.get((Calendar.HOUR_OF_DAY)), calender.get(Calendar.MINUTE), true);
}