您好我正在尝试实现aChartEngine库,当我在代码中添加值时(如本教程中by JavaCodeGeeks),它可以正常工作但如果我使用函数应用程序崩溃并在mCurrentSeries.add上提供NullPointerExepction( x,y)。当我调试以查看问题mCurrentSeries.add(x,y)重复两次(在同一循环中)然后崩溃。
这是班级
package com.example.achartengine;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import com.towel.math.Expression;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private GraphicalView mChart;
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private XYSeries mCurrentSeries;
private XYSeriesRenderer mCurrentRenderer;
private Expression function;
private EditText textFunction;
public void initChart() {
mCurrentSeries = new XYSeries("Sample Data");
mDataset.addSeries(mCurrentSeries);
mCurrentRenderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(mCurrentRenderer);
}
View.OnClickListener handler = new View.OnClickListener() {
@Override
public void onClick(View v) {
addGraphData();
}
};
private void addGraphData(){
String f = textFunction.getText().toString();
function = new Expression(f);
Double y;
for (int x = 0; x < 15; x++){
function.setVariable("x", x);
y = function.resolve();
mCurrentSeries.add(x, y);
}
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if(mChart==null){
initChart();
mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f);
layout.addView(mChart);
} else {
mChart.repaint();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button draw = (Button) findViewById(R.id.buttonDraw);
draw.setOnClickListener(handler);
textFunction = (EditText) findViewById(R.id.textFunction);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是logcat
03-10 08:11:24.609: D/AndroidRuntime(2450): Shutting down VM
03-10 08:11:24.609: W/dalvikvm(2450): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-10 08:11:24.619: E/AndroidRuntime(2450): FATAL EXCEPTION: main
03-10 08:11:24.619: E/AndroidRuntime(2450): java.lang.NullPointerException
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.example.achartengine.MainActivity.addGraphData(MainActivity.java:54)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.example.achartengine.MainActivity.access$0(MainActivity.java:46)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.example.achartengine.MainActivity$1.onClick(MainActivity.java:42)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.view.View.performClick(View.java:4204)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.view.View$PerformClick.run(View.java:17355)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.os.Handler.handleCallback(Handler.java:725)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.os.Looper.loop(Looper.java:137)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 08:11:24.619: E/AndroidRuntime(2450): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 08:11:24.619: E/AndroidRuntime(2450): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 08:11:24.619: E/AndroidRuntime(2450): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
mCurrentSeries = new XYSeries("Sample Data");
未在您收到错误的行之前调用。
答案 1 :(得分:2)
在initChart
中使用mCurrentSeries
之前,您需要致电onClick
以初始化{{1}}。