2017-10-22 19 views
1

私はデバイスをサイレントモードにして、ユーザーが設定した所定の時間に正常に戻すアプリを開発しました。ユーザーはイベントの名前を入力し、開始時刻と終了時刻を入力してから、作成ボタンをクリックしてイベントを作成します。私はアプリを実行し、ユーザーがこの時点で私のアプリがクラッシュし、私が理解できない例外をスローする開始時刻を選択します。私は以下の関連コードと例外エラーを掲示しています。例外エラー

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 /プロセス:信号を送信します。 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

あなたの例外スタックトレースがある?? –

+1

エラーをログにテキストとして投稿する必要があります。 –

+0

@DimaKozhevin私はログを追加しました。それは良くありませんが、間違いがどこにあるか教えてください。 –

答えて

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

ありがとうございました。私はその問題を手放してくれてありがとうございます。 –

+0

しかし、どういうわけか私のデバイスは、その時には静かになりませんでした。 –

+0

私はnewTime.gettimeinmillisとして保留中のインテントにnewTimeを渡しています –