当我在 Android 的 LineGraphView 对象上使用 addPoint() 方法时出现 StackOverFlowError
StackOverFlowError when I use addPoint() method on LineGraphView object in Android
// Initializing the LineGraphView object and adding it to linLayout
graph = new LineGraphView(rootView.getContext(), 100, Arrays.asList("x", "y", "z"));
linLayout.addView(graph);
graph.setVisibility(View.VISIBLE);
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
// Obtaining accelerometer values
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
float pythagorean = (float)Math.sqrt(x*x + y*y + z*z);
List<Float> graph_points = new ArrayList<Float>();
graph_points.add(pythagorean);
output.setText("\npythagorean\n"
+ "pythagorean: " + String.valueOf(pythagorean));
// Adding the current accelerometer values to the LinearGraphView object
graph.addPoint(graph_points);
}
}
graph.addPoint(graph_points);
//我收到一个 Whosebug 错误,它一直在向图形添加一个点并且 Android 应用程序崩溃了。
graph.addPoint(event.values);
//这没有给出计算器错误,它很好地绘制了图表上的 x、y、z。
我不明白的是,event.values
对象与我创建的 graph_points
对象是同一类型。它们都是 List<Float>
java 对象。为什么 event.values
正常工作而 graph_points
使应用程序崩溃?
// LineGraphView.java --> From LineGraphView Lib, I didn't write this!
/**
* Adds a set of datapoints for the next x value. The data points should be in the same
* order as the array of labels this object was initialized with.
* @param y The List of datapoints.
*/
public void addPoint(float[] y)
{
points.add(y.clone());
if(points.size() > maxDataWidth)
points.remove(0);
invalidate();
}
public void addPoint(List<Float> y)
{
float[] floats = new float[y.size()];
for(int i = 0; i < y.size(); i++){
floats[i] = y.get(i);
}
addPoint(y);
}
A WhosebugError
当你调用了太多的方法而没有返回时发生。在这种情况下,addPoint()
会无限调用自己。当一个方法调用自身时,它被称为递归。我不知道 addPoint()
应该做什么,但我非常怀疑它应该是递归的。如果递归是有意的,问题是你没有创建一个基本案例来确定递归何时停止。
// Initializing the LineGraphView object and adding it to linLayout
graph = new LineGraphView(rootView.getContext(), 100, Arrays.asList("x", "y", "z"));
linLayout.addView(graph);
graph.setVisibility(View.VISIBLE);
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
// Obtaining accelerometer values
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
float pythagorean = (float)Math.sqrt(x*x + y*y + z*z);
List<Float> graph_points = new ArrayList<Float>();
graph_points.add(pythagorean);
output.setText("\npythagorean\n"
+ "pythagorean: " + String.valueOf(pythagorean));
// Adding the current accelerometer values to the LinearGraphView object
graph.addPoint(graph_points);
}
}
graph.addPoint(graph_points);
//我收到一个 Whosebug 错误,它一直在向图形添加一个点并且 Android 应用程序崩溃了。
graph.addPoint(event.values);
//这没有给出计算器错误,它很好地绘制了图表上的 x、y、z。
我不明白的是,event.values
对象与我创建的 graph_points
对象是同一类型。它们都是 List<Float>
java 对象。为什么 event.values
正常工作而 graph_points
使应用程序崩溃?
// LineGraphView.java --> From LineGraphView Lib, I didn't write this!
/**
* Adds a set of datapoints for the next x value. The data points should be in the same
* order as the array of labels this object was initialized with.
* @param y The List of datapoints.
*/
public void addPoint(float[] y)
{
points.add(y.clone());
if(points.size() > maxDataWidth)
points.remove(0);
invalidate();
}
public void addPoint(List<Float> y)
{
float[] floats = new float[y.size()];
for(int i = 0; i < y.size(); i++){
floats[i] = y.get(i);
}
addPoint(y);
}
A WhosebugError
当你调用了太多的方法而没有返回时发生。在这种情况下,addPoint()
会无限调用自己。当一个方法调用自身时,它被称为递归。我不知道 addPoint()
应该做什么,但我非常怀疑它应该是递归的。如果递归是有意的,问题是你没有创建一个基本案例来确定递归何时停止。