我开发了一个折线图,其中我根据系统日期输入一个值,并使用achartengine库绘制折线图。
当我通过以相反顺序更改日期来输入值时,我面临崩溃问题。 即如果我在2014年6月6日进入2014年6月5日的价值,依此类推到6月1日。
请帮忙......
我得到了异常的Arrayindex赞:
06-06 15:06:35.634: E/AndroidRuntime(579): FATAL EXCEPTION: main
06-06 15:06:35.634: E/AndroidRuntime(579): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
06-06 15:06:35.634: E/AndroidRuntime(579): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-06 15:06:35.634: E/AndroidRuntime(579): at java.util.ArrayList.get(ArrayList.java:311)
06-06 15:06:35.634: E/AndroidRuntime(579): at org.achartengine.util.IndexXYMap.getYByIndex(IndexXYMap.java:86)
06-06 15:06:35.634: E/AndroidRuntime(579): at org.achartengine.model.XYSeries.getY(XYSeries.java:203)
06-06 15:06:35.634: E/AndroidRuntime(579): at org.achartengine.chart.XYChart.drawChartValuesText(XYChart.java:590)
06-06 15:06:35.634: E/AndroidRuntime(579): at org.achartengine.chart.XYChart.drawSeries(XYChart.java:534)
06-06 15:06:35.634: E/AndroidRuntime(579): at org.achartengine.chart.XYChart.draw(XYChart.java:311)
06-06 15:06:35.634: E/AndroidRuntime(579): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.View.draw(View.java:6880)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.View.draw(View.java:6883)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.View.draw(View.java:6883)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.View.draw(View.java:6883)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.widget.FrameLayout.draw(FrameLayout.java:357)
06-06 15:06:35.634: E/AndroidRuntime(579): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewRoot.draw(ViewRoot.java:1522)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.os.Looper.loop(Looper.java:130)
06-06 15:06:35.634: E/AndroidRuntime(579): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-06 15:06:35.634: E/AndroidRuntime(579): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 15:06:35.634: E/AndroidRuntime(579): at java.lang.reflect.Method.invoke(Method.java:507)
06-06 15:06:35.634: E/AndroidRuntime(579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-06 15:06:35.634: E/AndroidRuntime(579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-06 15:06:35.634: E/AndroidRuntime(579): at dalvik.system.NativeStart.main(Native Method)
这是我试图绘制图形的代码:
GraphicalView mChartView = null;
try
{
int y = 0;
length = 0;
int days_in_month1 = 0;
String strDaysSaved[] = null;
strDaysSaved = getAge();
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYSeriesRenderer TimeRenderer = new XYSeriesRenderer();
// TimeRenderer.setColor(getResources().getColor(color.holo_red_light));
TimeRenderer.setColor(Color.WHITE);
TimeRenderer.setPointStyle(PointStyle.CIRCLE);
TimeRenderer.setPointStrokeWidth(8.0f);
TimeRenderer.setFillPoints(false);
TimeRenderer.setLineWidth(5.0f);// Integer.parseInt(getResources().getString(R.string.lineSize)));
TimeRenderer.setDisplayChartValues(true);
XYSeriesRenderer InnerCircle = new XYSeriesRenderer();
;
InnerCircle.setColor(getResources().getColor(R.color.pointcolor));
InnerCircle.setPointStyle(PointStyle.CIRCLE);
InnerCircle.setPointStrokeWidth(8.0f);
InnerCircle.setFillPoints(true);
InnerCircle.setLineWidth(0.1f);
renderer = new XYMultipleSeriesRenderer();
if (strDaysSaved.length == 0)
{
showEmptyGraph();
}
setMainRendererProperties();
arrayxvalues = new ArrayList<String>();// dates
arrayxvalues = db.getmonths("dateadded");// group of months
ArrayList<String> arrayxvalues1 = new ArrayList<String>();// dates
for (int k = 0; k < arrayxvalues.size(); k++)
{
days_in_month1 = 0;
String month = formatDate("MMM", getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd"));
int year = Integer.parseInt(formatDate("yyyy", getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd")));
boolean isleapyear;
if ((year % 4 == 0) && year % 100 != 0)
{
isleapyear = true;
}
else if (year % 400 == 0)
{
isleapyear = true;
}
else
{
isleapyear = false;
}
arrayxvalues1 = db
.getmooddatesbymont(formatDate("yyyy-MM", getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd")));// gruop of months
int days_in_month = daysInMonth(
Integer.parseInt(formatDate("MM", getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd"))), isleapyear);
String year1 = formatDate("yyyy", getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd"));
for (int d = 0; d < days_in_month; d++)
{
length = length + 1;
renderer.addXTextLabel(length, d + 1 + "\n" + month); // formatDate("dd\nMMM", getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd")));
lenghts.add(length);
labels.add(d + 1 + "-" + month + "-" + year1);
xlabels.add(d + 1 + "-" + month);
}
for (int lb = 0; lb < arrayxvalues1.size(); lb++)
{
for (y = 0; y < labels.size(); y++)
{
String currlabl = formatDate("d-MMM", getCalendarFromString(arrayxvalues1.get(lb), "MM-dd"));// monthly label
String dat = formatDate("d", getCalendarFromString(arrayxvalues1.get(lb), "MM-dd"));
if (currlabl.equals(xlabels.get(y)))
{
int g1 = 0;// ,days=0;
if (k != 0)
{
year = Integer.parseInt(formatDate("yyyy",
getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd")));
if ((year % 4 == 0) && year % 100 != 0)
{
isleapyear = true;
}
else if (year % 400 == 0)
{
isleapyear = true;
}
else
{
isleapyear = false;
}
g1 = g1 + Integer.parseInt(Mood.getString(findDays, "0"));
days_in_month1 = g1
+ daysInMonth(
Integer.parseInt(formatDate("MM",
getCalendarFromString(arrayxvalues.get(k), "yyyy-MM-dd"))),
isleapyear);
plotx.add(lenghts.get(g1 + Integer.parseInt(dat)));
}
else
{
year = Integer.parseInt(formatDate("yyyy",
getCalendarFromString(arrayxvalues.get(0), "yyyy-MM-dd")));
if ((year % 4 == 0) && year % 100 != 0)
{
isleapyear = true;
}
else if (year % 400 == 0)
{
isleapyear = true;
}
else
{
isleapyear = false;
}
days_in_month1 = 0;
days_in_month1 = g1
+ daysInMonth(
Integer.parseInt(formatDate("MM",
getCalendarFromString(arrayxvalues.get(0), "yyyy-MM-dd"))),
isleapyear);
if (lenghts.size() == Integer.parseInt(dat))
plotx.add(lenghts.get(g1 + Integer.parseInt(dat) - 1) + 1);
else
plotx.add(lenghts.get(g1 + Integer.parseInt(dat)));
}
}
}
}
SharedPreferences.Editor editor1 = MoodDairy.edit();
editor1.putString(findDays, String.valueOf(days_in_month1));
editor1.commit();
}
renderer.setPanLimits(new double[]
{ 0, days_in_month1, 0, 8 });// [panMinimumX, panMaximumX, panMinimumY, panMaximumY]
String firstdate = "";
if (labels.size() != 0)
{
firstdate = labels.get(0).toString();
}
String[] months = db.getmonthsmood();// 01-12
if (!firstdate.equals(""))
{
for (int h = 0; h < months.length; h++)
{
renderer.setPointSize(15f);
if (h == 0)
{
dataset.addSeries(addSystolicUserSeries(months[h], h, ""));
renderer.addSeriesRenderer(TimeRenderer);
}
else
{
dataset.addSeries(addSystolicUserSeries(months[h], h, months[h - 1]));
renderer.addSeriesRenderer(TimeRenderer);
}
}
}
// To draw yellow circle inside
if (!firstdate.equals(""))
{
for (int h = 0; h < months.length; h++)
{
renderer.setPointSize(5f);
if (h == 0)
{
dataset.addSeries(addSystolicUserSeries(months[h], h, ""));
renderer.addSeriesRenderer(InnerCircle);
}
else
{
dataset.addSeries(addSystolicUserSeries(months[h], h, months[h - 1]));
renderer.addSeriesRenderer(InnerCircle);
}
}
}
if (arrayxvalues.size() > 0)
txtvwgraphtitle.setText(formatDate("MMMM yyyy", getCalendarFromString(arrayxvalues.get(0), "yyyy-MM-dd")));
calculaterange();
mChartView = ChartFactory.getTimeChartView(this, dataset, renderer, "");
mChartView.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
});
}
catch (NumberFormatException e)
{
}
catch (Exception e)
{
}
return mChartView;
答案 0 :(得分:3)
非常感谢您对我的问题感兴趣 我找到了解决问题的方法 我设置了这个属性:
XYSeriesRenderer TimeRenderer = new XYSeriesRenderer();
TimeRenderer.setDisplayChartValues(true);
删除TimeRenderer.setDisplayChartValues(true);这条线没有发生崩溃。
再次感谢。 :)
答案 1 :(得分:0)
我怀疑错误可能发生在:
if (lenghts.size() == Integer.parseInt(dat))
plotx.add(lenghts.get(g1 + Integer.parseInt(dat) - 1) + 1);
else
plotx.add(lenghts.get(g1 + Integer.parseInt(dat)));
请检查!