0

嗨,我正在尝试实现 aChartEngine 库,当我在代码中添加值时(就像在JavaCodeGeeks的本教程中一样)它工作得很好,但是如果我使用一个函数,应用程序会崩溃并给出NullPointerExceptionmCurrentSeries.add(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)
4

2 回答 2

2
    mCurrentSeries = new XYSeries("Sample Data");

在您收到错误的那一行之前没有被调用。

于 2013-03-10T08:28:57.367 回答
2

在使用它之前,您需要调用它initChart来初始化.mCurrentSeriesonClick

于 2013-03-10T08:30:16.070 回答