我正在构建一个带有AChartEngine的Android应用,并且会出现奇怪的错误。
在我为已建立的图表添加新点并触摸/平移图表后,我在NullPointerException
中得到LineChart(XYChart).getSeriesAndPointForScreenCoordinate(Point)
,其中GraphicalView
为变量oldX
和oldY
被发现为null。这些变量在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
为空,因为 clickableAreas.get(0)
以某种方式返回null clickableAreas 的集合。
我将尝试通过检查修补此问题以查看clickableArea是否为空,但这引出了一个问题:我是否正确添加了点数?
答案 0 :(得分:1)
根据堆栈跟踪,异常发生在以下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
&amp; oldY
都在GraphicalView.java中声明为原始float
。如果它们是原始的,它们怎么可能是null
?
答案 1 :(得分:0)
空指针异常可能是由于您的AchartEngine jar文件有时会在不再使用时失去使用。因此,您应该通过右键单击项目来检查构建路径上的jar文件。从那里选择选项
Order and Export->choose your jar files->click up and down button.
它可用于打开项目中使用的jar文件。