2017-07-29 8 views
0

現在の曜日と、曜日と日付に応じてIntegerを変更する現在の時刻を取得する必要があるアプリケーションを開発しています。Calendar.DAY_OF_WEEKはうまく動作しません - ANDROID

私はこれを作成しましたが、問題はほとんどありません。 火曜日の正午にフリーズします。 それは私にこの::

Unexpected constant; 
expected one of: Calendar.FRIDAY, Calendar.MONDAY, Calendar.SATURDAY, Calendar.SUNDAY, Calendar.THURSDAY, Calendar.TUESDAY, Calendar.WEDNESDAY less... 
This check warns if a switch statement does not explicitly include all the 
values declared by the typedef @IntDef declaration. 

を与え、私はそれを修正する方法がわかりません。

私のコードがあります: 私はこれを書いたとき、それは火曜日を除く作品:私はこれを作ったが、それは私のアプリをフリーズ

int countuser = 0; 
       int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK); 
       int timeOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 

       switch (day) { 
        case Calendar.FRIDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 0; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 1; 
         } 
         break; 
        case Calendar.MONDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 2; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 3; 
         } 
         break; 
        case Calendar.SATURDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 4; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 5; 
         } 
         break; 
        case Calendar.SUNDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 6; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 7; 
         } 
         break; 
        case Calendar.THURSDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 8; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 9; 
         } 
         break; 
        case Calendar.TUESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 10; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 11; 
         } 
         break; 
        case Calendar.WEDNESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 12; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 13; 
         } 
         break; 
       } 

しかし、私は一日に依存countuserをソートしたいと思います:このため

switch (day) { 
        case Calendar.FRIDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 8; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 9; 
         } 
         break; 
        case Calendar.MONDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 0; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 1; 
         } 
         break; 
        case Calendar.SATURDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 10; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 11; 
         } 
         break; 
        case Calendar.SUNDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 12; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 13; 
         } 
         break; 
        case Calendar.THURSDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 6; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 7; 
         } 
         break; 
        case Calendar.TUESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 2; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 3; 
         } 
         break; 
        case Calendar.WEDNESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 4; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 5; 
         } 
         break; 
       } 

と同じ、私は "ケースCalendar.MONDAYを" 並べ替えしようとすると:

switch (day) { 
        case Calendar.MONDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 0; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 1; 
         } 
         break; 
        case Calendar.TUESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 2; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 3; 
         } 
         break; 
        case Calendar.WEDNESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 4; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 5; 
         } 
         break; 

        case Calendar.THURSDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 6; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 7; 
         } 
         break; 
        case Calendar.FRIDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 8; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 9; 
         } 
         break; 

        case Calendar.SATURDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 10; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 11; 
         } 
         break; 
        case Calendar.SUNDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 12; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 13; 
         } 


       } 

これで、現在の曜日と時刻を取得してIntegerの値を変更するにはどうすればよいですか?私はたくさんのことを試しましたが、それは凍結するか、それとも一日のうちにうまくいかないか...

ありがとう。

編集:ここでは

は私のインポートのリストです:

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.AsyncTask; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AlertDialog; 
import android.text.Html; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.SimpleAdapter; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.Toast; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.HashMap; 
+0

正確なエラーは何ですか。整数の 'switch'ステートメントは、私にはうまくいくように見えます。 –

+0

@TimBiegeleisen問題は私が正確なエラーを知らないのですが、私が知っている唯一のことは、このコードはBackgroundWorker AsyncTaskにあり、この整数は日に応じてArrayListをソートするのに役立ちますが、 2つの最後のケースでは、backgroundWorkerがクラッシュし、アプリケーションがフリーズしてエラーが表示されませんが、backgroundWorkerがonPostExecuteを実行できないため動作しませんでした。 –

+0

@TimBiegeleisenしかし、私が記事で言ったように、switch文はUnexpected定数であるため、これが原因であるかどうかわかりません –

答えて

0

私がコメントするのに十分な担当者を持っていないが、あなたはあなたの輸入を投稿できますか?そしてtry-catchブロックの中に入れてみてください。

EDIT:

try-catchブロックがあなたのコードによってスローされる可能性キャッチに例外をしようとします。

try { 
     switch (day) { 
        case Calendar.FRIDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 0; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 1; 
         } 
         break; 
        case Calendar.MONDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 2; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 3; 
         } 
         break; 
        case Calendar.SATURDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 4; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 5; 
         } 
         break; 
        case Calendar.SUNDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 6; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 7; 
         } 
         break; 
        case Calendar.THURSDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 8; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 9; 
         } 
         break; 
        case Calendar.TUESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 10; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 11; 
         } 
         break; 
        case Calendar.WEDNESDAY: 
         if (timeOfDay > 22 || timeOfDay < 16) { 
          countuser = 12; 
         } else if (timeOfDay >= 16 || timeOfDay <= 22) { 
          countuser = 13; 
         } 
         break; 
     } 
    } catch(Exception e){ 
     e.printStackTrace(); 
    } 
+0

私はメインポストにインポートしました。そして、私はそれをtry-catchブロックの中に入れる方法を正確には分かっていません。私を助けてくれますか? –

+0

私の答えを編集しました。このコードは私のシステムでは完全に実行されますが、私はあなたがきれいに再建することを試みたことを願っています。 – Kiya

+0

それは再建されたと働いた...私はとても残念だが、ありがとう! –

関連する問題