异常错误

时间:2017-10-22 11:21:43

标签: java android broadcastreceiver android-pendingintent

我开发了一款应用程序,可以将设备置于静音模式,并在用户设定的给定时间内恢复正常。用户将输入事件的名称,然后输入开始和结束时间,然后单击“创建”按钮以创建事件。我运行应用程序,用户选择此点的开始时间我的应用程序崩溃并抛出一个我无法理解的异常。我发布了下面的相关代码和异常错误。

snapshot of the exception

异常错误

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:发送信号。 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);
}}

1 个答案:

答案 0 :(得分:0)

按照这种方式尝试所需的流程:

    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);
}