我正在使用AChartEngine构建一个Android应用,并且遇到了一些奇怪的错误。
在将新点添加到已经建立的图表并触摸/平移图表后,在NullPointerException
中得到一个LineChart(XYChart).getSeriesAndPointForScreenCoordinate(Point)
,其中GraphicalView
的变量oldX
和oldY
被发现为空。这些变量在GraphicalView.onTouchEvent()
中设置,但是显然没有被调用。
初始化图形时,是否可以手动触发onTouchEvent()
,还是可以解决此错误?
这是我的图表设置代码:
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
double[] xy = mChartView.toRealPoint(0);
if (seriesSelection == null) {
Toast.makeText(viewer.this,
"No chart element was clicked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
viewer.this,
"Chart element clicked", Toast.LENGTH_SHORT)
.show();
}
}
});
这是我添加新点的方法:
protected class NewPoints extends AsyncTask<Double, Void, Void> {
@Override
protected Void doInBackground(Double... values) {
mCurrentSeries.add(values[0], values[1]); // x, y
if (mChartView != null) {
mChartView.repaint();
}
return null;
}
}
我的测试代码:
for(double i = 0; i < 10; i++) {
new NewPoints().execute(i, Math.log(i));
}
这是LogCat的输出:
07-20 18:46:12.570: E/AndroidRuntime(13800): FATAL EXCEPTION: main
07-20 18:46:12.570: E/AndroidRuntime(13800): java.lang.NullPointerException
07-20 18:46:12.570: E/AndroidRuntime(13800): at org.achartengine.chart.XYChart.getSeriesAndPointForScreenCoordinate(XYChart.java:840)
07-20 18:46:12.570: E/AndroidRuntime(13800): at org.achartengine.GraphicalView.getCurrentSeriesAndPoint(GraphicalView.java:135)
07-20 18:46:12.570: E/AndroidRuntime(13800): at com.maximz.viewer$1.onClick(viewer.java:185)
07-20 18:46:12.570: E/AndroidRuntime(13800): at android.view.View.performClick(View.java:3511)
07-20 18:46:12.570: E/AndroidRuntime(13800): at android.view.View$PerformClick.run(View.java:14110)
07-20 18:46:12.570: E/AndroidRuntime(13800): at android.os.Handler.handleCallback(Handler.java:605)
07-20 18:46:12.570: E/AndroidRuntime(13800): at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 18:46:12.570: E/AndroidRuntime(13800): at android.os.Looper.loop(Looper.java:137)
07-20 18:46:12.570: E/AndroidRuntime(13800): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-20 18:46:12.570: E/AndroidRuntime(13800): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 18:46:12.570: E/AndroidRuntime(13800): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 18:46:12.570: E/AndroidRuntime(13800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-20 18:46:12.570: E/AndroidRuntime(13800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-20 18:46:12.570: E/AndroidRuntime(13800): at dalvik.system.NativeStart.main(Native Method)
更新1:
我重新编译了AChartEngine源代码,调试显示此方法中发生了错误:
public SeriesSelection getSeriesAndPointForScreenCoordinate(final Point screenPoint) {
if (clickableAreas != null)
for (int seriesIndex = clickableAreas.size() - 1; seriesIndex >= 0; seriesIndex--) {
// series 0 is drawn first. Then series 1 is drawn on top, and series 2
// on top of that.
// we want to know what the user clicked on, so traverse them in the
// order they appear on the screen.
int pointIndex = 0;
if (clickableAreas.get(seriesIndex) != null) {
RectF rectangle;
for (ClickableArea area : clickableAreas.get(seriesIndex)) {
rectangle = area.getRect(); // EXCEPTION HERE: area is null
if (rectangle != null && rectangle.contains(screenPoint.getX(), screenPoint.getY())) {
return new SeriesSelection(seriesIndex, pointIndex, area.getX(), area.getY());
}
pointIndex++;
}
}
}
return super.getSeriesAndPointForScreenCoordinate(screenPoint);
}
在
rectangle = area.getRect();
行中抛出异常,其中area
为null,因为clickableAreas.get(0)
以某种方式返回null clickableAreas的集合。我将尝试通过检查是否clickableArea为null来对此进行修补,但这引出了一个问题:我是否正确添加了点?
最佳答案
根据堆栈跟踪,异常发生在XYChart.java的以下代码的第二行(如果我的源代码具有匹配的行号):
for (ClickableArea area : clickableAreas.get(seriesIndex)) {
rectangle = area.getRect(); // <--- Exception here!
if (rectangle != null && rectangle.contains(screenPoint.getX(), screenPoint.getY())) {
return new SeriesSelection(seriesIndex, pointIndex, area.getX(), area.getY());
}
pointIndex++;
}
我不知道怎么回事。
您可以使用AChartEngine的源代码进行编译和调试吗?
进一步的
oldX
和oldY
都在GraphicalView.java中声明为原始float
。如果它们是原语,怎么能成为null
?