我有一个错误,我从昨天起就开始寻找它。它似乎是阵列中的一个大小的问题。 当异常似乎发生时,这是函数:
private XYMultipleSeriesDataset getTruitonBarDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int [] classObject =new int[] {-70,-56,-45,-80,-47,-55,-80,-50,-75,-85,-35,-45};//Signal strength receiver from each access point
for (int j=0;j<classObject.length;j++){
values.add(new int[] { classObject[j]}); //every serie will concern an access point and contain one RSSI
}
final int nr = classObject.length;
ArrayList<String> legendTitles = new ArrayList<String>();
for (int k = 0; k < classObject.length; k++) {
legendTitles.add("Sales"+k);//every serie will have a legend
}
SERIES_NR = classObject.length ;//number of access point
for (int i = 0; i < SERIES_NR; i++) {//Loop for every series
CategorySeries series = new CategorySeries(legendTitles.get(i));
int[] v = values.get(i);
int seriesLength = v.length;
dataset.addSeries(series.toXYSeries());
for (int k = 0; k < seriesLength; k++) {
Log.i("STOOP SERIES_NR",SERIES_NR+"");
Log.i("STOOP ii",i+"");
Log.i("STOOP",k+"");
Log.i("STOOP",v[k]+"");
Log.i("STOOP",v[k] / 10+"");
/* XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 40 - v[k] %10);*/
switch (v[k] / 10){
case -8 :
//series.add(classObject[k]);
if (v[k] %10 == 0)
//series.add(40);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 40);
}
else
//series.add(40 - v[k] %10);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 40 - v[k] %10);
}
break;
case -7 :
//series.add(classObject[k]);
if (v[k] %10 == 0)
{
XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 50);
tempSerie.addAnnotation("text *** "+i, 0, 50);
}
else // EDITED
//series.add(50 - v[k] %10);
{XYSeries tempSerie = dataset.getSeriesAt(i);
// ERROR LINE 100 tempSerie.add(i, 7.5,50 - v[k] %10);
tempSerie.add(i,50 - v[k] %10);//right line
// series.toXYSeries().add(7.5,50 - v[k] %10);
}
break;
case -6 :
//series.add(classObject[k]);
if (v[k] %10 == 0)
//series.add(60);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 60);
}
else
//series.add(60 - v[k] %10);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 60 - v[k] %10);
}
break;
case -5 :
//series.add(classObject[k]);
if (v[k] %10 == 0)
//series.add(70);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 70);
}
else
//series.add(70 - v[k] %10);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 70 - v[k] %10);
}
break;
case -4 :
//series.add(classObject[k]);
if (v[k] %10 == 0)
//series.add(80);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 80);
}
else
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 80 - v[k] %10);
tempSerie.addAnnotation("text"+i, i, 80 - v[k] %10);
}
break;
case -3 :
//series.add(classObject[k]);
if (v[k] %10 == 0)
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 90);
}
else
//series.add(90 - v[k] %10);
{XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 90 - v[k] %10);
}
break;
}
}
}
return dataset;
}
当我删除开关条件部分并放入此代码时,我不会得到任何异常:
XYSeries tempSerie = dataset.getSeriesAt(i);
tempSerie.add(i, 40 - v[k] %10);
。我试着运行代码mannualy,我没有发现错误(也许我没有看到它)。 这里运行停止(Log.i)
04-11 08:58:24.415: I/STOOP(17738): -47
04-11 08:58:24.415: I/STOOP(17738): -4
04-11 08:58:24.415: I/STOOP SERIES_NR(17738): 12
04-11 08:58:24.415: I/STOOP ii(17738): 5
04-11 08:58:24.415: I/STOOP(17738): 0
04-11 08:58:24.415: I/STOOP(17738): -55
04-11 08:58:24.415: I/STOOP(17738): -5
04-11 08:58:24.416: I/STOOP SERIES_NR(17738): 12
04-11 08:58:24.416: I/STOOP ii(17738): 6
04-11 08:58:24.416: I/STOOP(17738): 0
04-11 08:58:24.416: I/STOOP(17738): -80
04-11 08:58:24.416: I/STOOP(17738): -8
04-11 08:58:24.416: I/STOOP SERIES_NR(17738): 12
04-11 08:58:24.416: I/STOOP ii(17738): 7
04-11 08:58:24.416: I/STOOP(17738): 0
04-11 08:58:24.416: I/STOOP(17738): -50
04-11 08:58:24.416: I/STOOP(17738): -5
04-11 08:58:24.416: I/STOOP SERIES_NR(17738): 12
04-11 08:58:24.416: I/STOOP ii(17738): 8
04-11 08:58:24.416: I/STOOP(17738): 0
04-11 08:58:24.416: I/STOOP(17738): -75
04-11 08:58:24.417: I/STOOP(17738): -7
这是我的例外:
04-11 08:58:24.431: W/dalvikvm(17738): threadid=1: thread exiting with uncaught exception (group=0x419559a8)
04-11 08:58:24.439: E/AndroidRuntime(17738): FATAL EXCEPTION: main
04-11 08:58:24.439: E/AndroidRuntime(17738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.truitonachartengine/com.example.truitonachartengine.TruitonAChartEngineActivity}: java.lang.IndexOutOfBoundsException: Invalid index 8, size is 0
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.ActivityThread.access$600(ActivityThread.java:162)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.os.Handler.dispatchMessage(Handler.java:107)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.os.Looper.loop(Looper.java:194)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.ActivityThread.main(ActivityThread.java:5371)
04-11 08:58:24.439: E/AndroidRuntime(17738): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 08:58:24.439: E/AndroidRuntime(17738): at java.lang.reflect.Method.invoke(Method.java:525)
04-11 08:58:24.439: E/AndroidRuntime(17738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-11 08:58:24.439: E/AndroidRuntime(17738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-11 08:58:24.439: E/AndroidRuntime(17738): at dalvik.system.NativeStart.main(Native Method)
04-11 08:58:24.439: E/AndroidRuntime(17738): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 8, size is 0
04-11 08:58:24.439: E/AndroidRuntime(17738): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-11 08:58:24.439: E/AndroidRuntime(17738): at java.util.ArrayList.add(ArrayList.java:143)
04-11 08:58:24.439: E/AndroidRuntime(17738): at org.achartengine.util.IndexXYMap.put(IndexXYMap.java:42)
04-11 08:58:24.439: E/AndroidRuntime(17738): at org.achartengine.model.XYSeries.add(XYSeries.java:155)
04-11 08:58:24.439: E/AndroidRuntime(17738): at com.example.truitonachartengine.TruitonAChartEngineActivity.getTruitonBarDataset(TruitonAChartEngineActivity.java:100)
04-11 08:58:24.439: E/AndroidRuntime(17738): at com.example.truitonachartengine.TruitonAChartEngineActivity.onCreate(TruitonAChartEngineActivity.java:37)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.Activity.performCreate(Activity.java:5122)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-11 08:58:24.439: E/AndroidRuntime(17738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-11 08:58:24.439: E/AndroidRuntime(17738): ... 11 more
我会感激任何帮助。谢谢。