在创建graphView()后得到此错误。我认为这与graphView()有关。奇怪的是,前一阵子一切正常,然后我添加了两个新的工作正常的活动。
这是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.weightogo, PID: 10342
java.lang.IllegalArgumentException: Illegal initial capacity: -4
at java.util.HashMap.<init>(HashMap.java:448)
at java.util.HashMap.<init>(HashMap.java:467)
at java.util.LinkedHashMap.<init>(LinkedHashMap.java:383)
at com.jjoe64.graphview.GridLabelRenderer.adjustHorizontal(GridLabelRenderer.java:947)
at com.jjoe64.graphview.GridLabelRenderer.adjustSteps(GridLabelRenderer.java:980)
at com.jjoe64.graphview.GridLabelRenderer.draw(GridLabelRenderer.java:1113)
at com.jjoe64.graphview.GraphView.drawGraphElements(GraphView.java:307)
at com.jjoe64.graphview.GridLabelRenderer.draw(GridLabelRenderer.java:1108)
at com.jjoe64.graphview.GraphView.drawGraphElements(GraphView.java:307)
at com.jjoe64.graphview.GraphView.onDraw(GraphView.java:336)
at android.view.View.draw(View.java:19192)
at android.view.View.updateDisplayListIfDirty(View.java:18142)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.support.constraint.ConstraintLayout.dispatchDraw(ConstraintLayout.java:1963)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.draw(View.java:19195)
at com.android.internal.policy.DecorView.draw(DecorView.java:788)
at android.view.View.updateDisplayListIfDirty(View.java:18142)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:669)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:675)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:783)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2992)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2806)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2359)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是使用graphView的函数:
public void buildGraph() {
MyDatabaseHelper dbHelper = new MyDatabaseHelper(MainActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ArrayList<Double> HiProfWang = userWeightTables.fetchWeight(db);
GraphView graph = (GraphView) findViewById(R.id.sexbot);
DataPoint[] dpvals = new DataPoint[HiProfWang.size()];
for (int i = 0; i < HiProfWang.size(); i++) {
dpvals[i] = new DataPoint(i + 1, HiProfWang.get(i));
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dpvals);
graph.getViewport().setYAxisBoundsManual(false);
if(HiProfWang.size() < 7){
graph.getViewport().setMinX(1);
}
else{
graph.getViewport().setMinX(HiProfWang.size() - 6);
}
graph.getViewport().setMaxX(HiProfWang.size());
graph.getViewport().setXAxisBoundsManual(true);
// enable scrolling
graph.getViewport().setScrollable(true); // horizontal scrolling
graph.addSeries(series);
displayWeightDifference(HiProfWang);
}
我已经调试了一段时间,但是除了在具有图形视图的活动的OnCreate()之后立即关闭应用程序之外,我没有看到任何错误。
这是此活动的Oncreate函数:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weightButton = (Button) findViewById(R.id.weightButton);
displayDifference = (TextView) findViewById(R.id.displayDifference);
displayAverage = (TextView) findViewById(R.id.displayAverage);
displayWeight = (TextView) findViewById(R.id.displayWeight);
ccButton = (Button) findViewById(R.id.ccButton);
initWeight();
initCalorieCounter();
displayUserWeight();
displayWeightAverage();
buildGraph();
}
答案 0 :(得分:0)
使用graphview库时出现此错误。对我来说,发生的情况是.setMaxX
小于.setMinX
,这是因为图形轴范围不能为负值。您需要确保用于设置轴的代码不允许.setMinX
大于.setMaxX
。你在哪里
if(HiProfWang.size() < 7){
graph.getViewport().setMinX(1);
}
如果setMaxX
返回0,则.setMinX(1)
可能小于HiProfWang.size()
。
我不确定minX和maxX相等时的行为。