[英]Calendar.DAY_OF_WEEK dosn't work fine - ANDROID
我正在開發一個應用程序,該應用程序需要獲取當前的星期幾和當前時間才能根據日期和日期更改Integer。
我已經做到了,但是我沒有什么問題:它在星期二中午凍結。 它給了我這個:
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;
}
但是我想根據日期對計數用戶進行排序,所以我這樣做了,但它凍結了我的應用程序:
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;
}
與此相同,當我嘗試對“ case 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;
我沒有足夠的代表對此發表評論,但是您可以發布您的進口商品嗎? 並嘗試將其放入try-catch塊中。
編輯:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.