CalendarView错误显示多个高亮
CalendarView Error to Display Multiple Highlights
如何根据计算突出显示多天
你好。请帮助我如何做到这一点。
package com.tgkjer.fertilitystress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CalendarAdapter extends BaseAdapter {
private Context mContext;
public CalendarMain main;
private java.util.Calendar month;
public GregorianCalendar pmonth; // calendar instance for previous month
/**
* calendar instance for previous month for getting complete view
*/
public GregorianCalendar pmonthmaxset;
private GregorianCalendar selectedDate;
int firstDay;
int maxWeeknumber;
int maxP;
int calMaxP;
int lastWeekDay;
int leftDays;
int mnthlength;
String itemvalue, curentDateString;
DateFormat df;
private ArrayList<String> items;
public static List<String> dayString;
private View previousView;
public CalendarAdapter(Context c, GregorianCalendar monthCalendar) {
CalendarAdapter.dayString = new ArrayList<String>();
Locale.setDefault(Locale.US);
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
mContext = c;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
public void setItems(ArrayList<String> items) {
for (int i = 0; i != items.size(); i++) {
if (items.get(i).length() == 1) {
items.set(i, "0" + items.get(i));
}
}
this.items = items;
}
public int getCount() {
return dayString.size();
}
public Object getItem(int position) {
return dayString.get(position);
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView) v.findViewById(R.id.date);
// separates daystring into parts.
String[] separatedTime = dayString.get(position).split("-");
// taking last part of date. ie; 2 from 2012-12-02
String gridvalue = separatedTime[2].replaceFirst("^0*", "");
// checking whether the day is in current month or not.
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
// setting offdays to white color.
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting curent month's days in blue color.
dayView.setTextColor(Color.BLUE);
}
if (dayString.get(position).equals(curentDateString)) {
setSelected(v);
previousView = v;
} else {
v.setBackgroundResource(R.drawable.list_item_background);
}
dayView.setText(gridvalue);
// create date string for comparison
String date = dayString.get(position);
if (date.length() == 1) {
date = "0" + date;
}
String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
//main.Calc();
/*String s = "";
long l = Long.parseLong(s);
DbFertility db = new DbFertility(getActivity());
db.open();
int cycle = Integer.parseInt(db.getCycle(l));
int period = Integer.parseInt(db.getPeriod(l));
int date1 = Integer.parseInt(db.getPeriod(l));
db.close();*/
int periodCycleDays = 28;
long date1 = Long.parseLong(date);
int bleedingDays = 3;
int fertilePhaseStart = periodCycleDays - 20;
int fertilePhaseEnd = periodCycleDays - 11;
int ovulation = (fertilePhaseStart-1) + (fertilePhaseEnd - fertilePhaseStart)/2;
// show icon if date is not empty and it exists in the items array
ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
if (periodCycleDays == 28) {
for(long i = date1; i <= bleedingDays; i++ )
{
iw.setVisibility(View.VISIBLE);
}
for(int j = fertilePhaseStart; j < ovulation; j++)
{
int b = 6;
if(j == b)
{
iw.setVisibility(View.VISIBLE);
}else{
iw.setVisibility(View.VISIBLE);
}
}
}
return v;
}
private Context getActivity() {
// TODO Auto-generated method stub
return null;
}
public View setSelected(View view) {
if (previousView != null) {
previousView.setBackgroundResource(R.drawable.list_item_background);
}
previousView = view;
view.setBackgroundResource(R.drawable.calendar_cel_selectl);
return view;
}
public void refreshDays() {
// clear items
items.clear();
dayString.clear();
Locale.setDefault(Locale.US);
pmonth = (GregorianCalendar) month.clone();
// month start day. ie; sun, mon, etc
firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
// finding number of weeks in current month.
maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
// allocating maximum row number for the gridview.
mnthlength = maxWeeknumber * 7;
maxP = getMaxP(); // previous month maximum day 31,30....
calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
/**
* Calendar instance for getting a complete gridview including the three
* month's (previous,current,next) dates.
*/
pmonthmaxset = (GregorianCalendar) pmonth.clone();
/**
* setting the start date as previous month's required date.
*/
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);
/**
* filling calendar gridview.
*/
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
dayString.add(itemvalue);
}
}
private int getMaxP() {
int maxP;
if (month.get(GregorianCalendar.MONTH) == month.getActualMinimum(GregorianCalendar.MONTH)) {
pmonth.set((month.get(GregorianCalendar.YEAR) - 1), month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
pmonth.set(GregorianCalendar.MONTH, month.get(GregorianCalendar.MONTH) - 1);
}
maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
return maxP;
}
public void Calculation(){
}
}
03-01 08:43:52.412: E/AndroidRuntime(1294): FATAL EXCEPTION: main
03-01 08:43:52.412: E/AndroidRuntime(1294): Process: com.tgkjer.fertilitystress, PID: 1294
03-01 08:43:52.412: E/AndroidRuntime(1294): java.lang.NumberFormatException: Invalid long: "2015-03-01"
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.invalidLong(Long.java:124)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.parse(Long.java:361)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.parseLong(Long.java:352)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.parseLong(Long.java:318)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:137)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.GridView.onMeasure(GridView.java:1044)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.os.Handler.handleCallback(Handler.java:733)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.os.Handler.dispatchMessage(Handler.java:95)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.os.Looper.loop(Looper.java:136)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.reflect.Method.invoke(Method.java:515)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-01 08:43:52.412: E/AndroidRuntime(1294): at dalvik.system.NativeStart.main(Native Method)
“这就是我 logcat 中的错误 你能帮我指出错误在哪里吗
在我的代码中?
预期输出:我想根据周期的计算突出显示多天
FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid long: "2015-03-01"
当您解析任何字符串数字时会引发此异常(即 String stringNumber="09";
,其中字符串数字是双引号之间的任何数字,如“09”),而不是数字,例如包含其他字符的字符串数字如果它包含“0-9”、"A09"、"A9"、“”、“9a”、“9-1”等,则在数字中代替“9”或“09”。此异常将扔
您必须避免传递所有其他字符。
您只能将数字作为字符串传递,因此您将获得 int、long 等形式的数字
at com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:137)
在 `
中的第 90 行
com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:90)
String[] separatedTime = dayString.get(position).split("-");
separatedTime
将具有字符串编号,即 dayString
具有类似于 "2015-03-01"
的日期值,这就是为什么您使用分隔符“-”拆分值的原因“-”
您在
中的第 117 行呼叫
com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:117)
// create date string for comparison
String date = dayString.get(position);
所以日期的值为“2015-03-01”
if (date.length() == 1) {
date = "0" + date;
}
上面第 119 到 121 行的代码不会将日期值作为事实,因为 date.length() == 1
是 false
它不会进入 if 块。
现在请参阅
中的第 137 行
com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:137)
long date1 = Long.parseLong(date);
此处 Long.parseLong() 期望值类似于字符串编号,但得到的日期值为字符串“2015-03-01”,因此出现异常。
所以在
这样的日期使用你的拆分函数
String[] separatedyyMMdd = date.split("-");
long date1yy = Long.parseLong(separatedyyMMdd[0]);
long date1mm = Long.parseLong(separatedyyMMdd[1]);
long date1dd = Long.parseLong(separatedyyMMdd[2]);
它将解决您的上述问题
如何根据计算突出显示多天
你好。请帮助我如何做到这一点。
package com.tgkjer.fertilitystress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CalendarAdapter extends BaseAdapter {
private Context mContext;
public CalendarMain main;
private java.util.Calendar month;
public GregorianCalendar pmonth; // calendar instance for previous month
/**
* calendar instance for previous month for getting complete view
*/
public GregorianCalendar pmonthmaxset;
private GregorianCalendar selectedDate;
int firstDay;
int maxWeeknumber;
int maxP;
int calMaxP;
int lastWeekDay;
int leftDays;
int mnthlength;
String itemvalue, curentDateString;
DateFormat df;
private ArrayList<String> items;
public static List<String> dayString;
private View previousView;
public CalendarAdapter(Context c, GregorianCalendar monthCalendar) {
CalendarAdapter.dayString = new ArrayList<String>();
Locale.setDefault(Locale.US);
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
mContext = c;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
public void setItems(ArrayList<String> items) {
for (int i = 0; i != items.size(); i++) {
if (items.get(i).length() == 1) {
items.set(i, "0" + items.get(i));
}
}
this.items = items;
}
public int getCount() {
return dayString.size();
}
public Object getItem(int position) {
return dayString.get(position);
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView) v.findViewById(R.id.date);
// separates daystring into parts.
String[] separatedTime = dayString.get(position).split("-");
// taking last part of date. ie; 2 from 2012-12-02
String gridvalue = separatedTime[2].replaceFirst("^0*", "");
// checking whether the day is in current month or not.
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
// setting offdays to white color.
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting curent month's days in blue color.
dayView.setTextColor(Color.BLUE);
}
if (dayString.get(position).equals(curentDateString)) {
setSelected(v);
previousView = v;
} else {
v.setBackgroundResource(R.drawable.list_item_background);
}
dayView.setText(gridvalue);
// create date string for comparison
String date = dayString.get(position);
if (date.length() == 1) {
date = "0" + date;
}
String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
//main.Calc();
/*String s = "";
long l = Long.parseLong(s);
DbFertility db = new DbFertility(getActivity());
db.open();
int cycle = Integer.parseInt(db.getCycle(l));
int period = Integer.parseInt(db.getPeriod(l));
int date1 = Integer.parseInt(db.getPeriod(l));
db.close();*/
int periodCycleDays = 28;
long date1 = Long.parseLong(date);
int bleedingDays = 3;
int fertilePhaseStart = periodCycleDays - 20;
int fertilePhaseEnd = periodCycleDays - 11;
int ovulation = (fertilePhaseStart-1) + (fertilePhaseEnd - fertilePhaseStart)/2;
// show icon if date is not empty and it exists in the items array
ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
if (periodCycleDays == 28) {
for(long i = date1; i <= bleedingDays; i++ )
{
iw.setVisibility(View.VISIBLE);
}
for(int j = fertilePhaseStart; j < ovulation; j++)
{
int b = 6;
if(j == b)
{
iw.setVisibility(View.VISIBLE);
}else{
iw.setVisibility(View.VISIBLE);
}
}
}
return v;
}
private Context getActivity() {
// TODO Auto-generated method stub
return null;
}
public View setSelected(View view) {
if (previousView != null) {
previousView.setBackgroundResource(R.drawable.list_item_background);
}
previousView = view;
view.setBackgroundResource(R.drawable.calendar_cel_selectl);
return view;
}
public void refreshDays() {
// clear items
items.clear();
dayString.clear();
Locale.setDefault(Locale.US);
pmonth = (GregorianCalendar) month.clone();
// month start day. ie; sun, mon, etc
firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
// finding number of weeks in current month.
maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
// allocating maximum row number for the gridview.
mnthlength = maxWeeknumber * 7;
maxP = getMaxP(); // previous month maximum day 31,30....
calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
/**
* Calendar instance for getting a complete gridview including the three
* month's (previous,current,next) dates.
*/
pmonthmaxset = (GregorianCalendar) pmonth.clone();
/**
* setting the start date as previous month's required date.
*/
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);
/**
* filling calendar gridview.
*/
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
dayString.add(itemvalue);
}
}
private int getMaxP() {
int maxP;
if (month.get(GregorianCalendar.MONTH) == month.getActualMinimum(GregorianCalendar.MONTH)) {
pmonth.set((month.get(GregorianCalendar.YEAR) - 1), month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
pmonth.set(GregorianCalendar.MONTH, month.get(GregorianCalendar.MONTH) - 1);
}
maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
return maxP;
}
public void Calculation(){
}
}
03-01 08:43:52.412: E/AndroidRuntime(1294): FATAL EXCEPTION: main
03-01 08:43:52.412: E/AndroidRuntime(1294): Process: com.tgkjer.fertilitystress, PID: 1294
03-01 08:43:52.412: E/AndroidRuntime(1294): java.lang.NumberFormatException: Invalid long: "2015-03-01"
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.invalidLong(Long.java:124)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.parse(Long.java:361)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.parseLong(Long.java:352)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.Long.parseLong(Long.java:318)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:137)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.GridView.onMeasure(GridView.java:1044)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.View.measure(View.java:16497)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.os.Handler.handleCallback(Handler.java:733)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.os.Handler.dispatchMessage(Handler.java:95)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.os.Looper.loop(Looper.java:136)
03-01 08:43:52.412: E/AndroidRuntime(1294): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 08:43:52.412: E/AndroidRuntime(1294): at java.lang.reflect.Method.invoke(Method.java:515)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-01 08:43:52.412: E/AndroidRuntime(1294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-01 08:43:52.412: E/AndroidRuntime(1294): at dalvik.system.NativeStart.main(Native Method)
“这就是我 logcat 中的错误 你能帮我指出错误在哪里吗 在我的代码中?
预期输出:我想根据周期的计算突出显示多天
FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid long: "2015-03-01"
当您解析任何字符串数字时会引发此异常(即 String stringNumber="09";
,其中字符串数字是双引号之间的任何数字,如“09”),而不是数字,例如包含其他字符的字符串数字如果它包含“0-9”、"A09"、"A9"、“”、“9a”、“9-1”等,则在数字中代替“9”或“09”。此异常将扔
您必须避免传递所有其他字符。
您只能将数字作为字符串传递,因此您将获得 int、long 等形式的数字
at com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:137)
在 `
中的第 90 行com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:90)
String[] separatedTime = dayString.get(position).split("-");
separatedTime
将具有字符串编号,即 dayString
具有类似于 "2015-03-01"
的日期值,这就是为什么您使用分隔符“-”拆分值的原因“-”
您在
中的第 117 行呼叫com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:117)
// create date string for comparison
String date = dayString.get(position);
所以日期的值为“2015-03-01”
if (date.length() == 1) {
date = "0" + date;
}
上面第 119 到 121 行的代码不会将日期值作为事实,因为 date.length() == 1
是 false
它不会进入 if 块。
现在请参阅
中的第 137 行com.tgkjer.fertilitystress.CalendarAdapter.getView(CalendarAdapter.java:137)
long date1 = Long.parseLong(date);
此处 Long.parseLong() 期望值类似于字符串编号,但得到的日期值为字符串“2015-03-01”,因此出现异常。 所以在
这样的日期使用你的拆分函数String[] separatedyyMMdd = date.split("-");
long date1yy = Long.parseLong(separatedyyMMdd[0]);
long date1mm = Long.parseLong(separatedyyMMdd[1]);
long date1dd = Long.parseLong(separatedyyMMdd[2]);
它将解决您的上述问题