在使用表格中的可变值的数据绘制achartengine中的用户数据的问题

时间:2014-08-26 14:19:10

标签: java android graph charts achartengine

我正在使用achartengine在我的应用中绘制图表。我在table.xml的表格中声明与Table.java相关联的点。现在我的图表应该得到这些点并使用它们。我的图表以Graph.java编码,并通过Graphdisplay.java进行调用。

EDIT2:我稍微更改了代码。首先,我将Graphdisplay.java中的代码添加到Graph.java的onCreate方法中,并删除了Graphdisplay.java

其次我在Graph.java的第一个答案中添加了一些get和set方法,例如here,并在onCreate方法中从Table.java获取数据。

第三,我将get方法放在GraphicalView

Graph.java:

public class Graph extends Activity{

//only for x1 for now
public double ds_x1;
public Double getX1(){return this.ds_x1;}
public void setX1(Double ds_x1){this.ds_x1=ds_x1;}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graph);

    //getting the data from Table.java
    ds_x1 = getIntent().getExtras().getDouble("s_x1");

    Graph line = new Graph();
    final GraphicalView gView = line.getView(getApplicationContext());
    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    layout.addView(gView);

}

public GraphicalView getView(Context context) {

    Double d_x1 = getX1();

    ArrayList<Double> xList = new ArrayList<Double>();
    double x1 = d_x1;
    xList.add(x1);
    double x2 = 2;
    xList.add(x2);
    double x3 = 3;
    xList.add(x3);

    ArrayList<Double> yList = new ArrayList<Double>();
    double y1 = 1;
    yList.add(y1);
    double y2 = 2;
    yList.add(y2);
    double y3 = 3;
    yList.add(y3);

    XYSeries series = new XYSeries("List");
    for (int i = 0; i < xList.size(); i++) {
        series.add(i, xList.get(i), yList.get(i));
    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);

    XYSeriesRenderer renderer = new XYSeriesRenderer();

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

    return ChartFactory.getLineChartView(context, dataset, mRenderer);
    } 
}

Table.java:现在双倍没有字符串

 EditText x1 = (EditText) findViewById(R.id.x1);
 EditText x2 = (EditText) findViewById(R.id.x2);
 EditText x3 = (EditText) findViewById(R.id.x3);
 EditText y1 = (EditText) findViewById(R.id.y1);
 EditText y2 = (EditText) findViewById(R.id.y2);
 EditText y3 = (EditText) findViewById(R.id.y3);

 Button b_continue = (Button) findViewById(R.id.b_continue);
    b_continue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Double s_x1 = Double.parseDouble(x1.getText().toString());
            Double s_x2 = Double.parseDouble(x2.getText().toString());
            Double s_x3 = Double.parseDouble(x3.getText().toString());
            Double s_y1 = Double.parseDouble(y1.getText().toString());
            Double s_y2 = Double.parseDouble(y2.getText().toString());
            Double s_y3 = Double.parseDouble(y3.getText().toString());

            Intent myIntent = new Intent(view.getContext(), Graph.class);
            myIntent.putExtra("s_x1", s_x1);
            myIntent.putExtra("s_x2", s_x2);
            myIntent.putExtra("s_x3", s_x3);
            myIntent.putExtra("s_y1", s_y1);
            myIntent.putExtra("s_y2", s_y2);
            myIntent.putExtra("s_y3", s_y3);
            startActivity(myIntent);
        }
    });

logcat的:

  08-29 14:31:00.720  17322-17322/test.com.myApp E/AndroidRuntime﹕ FATAL EXCEPTION: main
  java.lang.RuntimeException: Unable to start activity 
  ComponentInfo{test.com.myApp/test.com.myApp.Graph}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at test.com.myApp.Graph.getView(Graph.java:96)
        at test.com.myApp.Graph.onCreate(Graph.java:44)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

问题:点击按钮进入图表时,应用程序崩溃,而不是显示带有值的图表!我发现问题在于它不是像double double = 5.0这样的定义值。 aChartEngine在表中输入的值存在问题。

1 个答案:

答案 0 :(得分:1)

还有一种方法可以为您的意图设置“双重额外”: Intent android.content.Intent.putExtra(String name,double value)

以及获得双倍额外费用的方法: double android.content.Intent.getDoubleExtra(String name,double defaultValue)

所以你不需要解析字符串......

但是,总的来说,这是正确的方法。或者你有特定的问题吗?

编辑: 使用它而不是valueOf:

double y2 = Double.parseDouble(getIntent()。getStringExtra(“s_y2”))

EDIT2,试试:

 Button b_continue = (Button) findViewById(R.id.b_continue);
 b_continue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        String s_x1 = x1.getText().toString();
        String s_x2 = x2.getText().toString();
        String s_x3 = x3.getText().toString();
        String s_y1 = y1.getText().toString();
        String s_y2 = y2.getText().toString();
        String s_y3 = y3.getText().toString();

        Intent myIntent2 = new Intent(view.getContext(), Graph.class);
        myIntent2.putExtra("s_x1", s_x1);
        myIntent2.putExtra("s_x2", s_x2);
        myIntent2.putExtra("s_x3", s_x3);
        myIntent2.putExtra("s_y1", s_y1);
        myIntent2.putExtra("s_y2", s_y2);
        myIntent2.putExtra("s_y3", s_y3);

        Intent myIntent = new Intent(view.getContext(), Graphdisplay.class);
        startActivity(myIntent);
    }
});