我想根据parse.com的数据创建构成图表的应用程序。我已经阅读了一些示例和教程,但仍然有显示图表的问题。以下是我的代码:
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import java.util.ArrayList;
public class LineGraph {
public ArrayList<Integer> dataArray;
XYMultipleSeriesDataset dataset;
XYMultipleSeriesRenderer renderer;
public static boolean ClickEnabled = true;
public Intent getIntent(Context context) {
ArrayList<Integer> y = this.dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
renderer.setPanEnabled(true, false);
renderer.setClickEnabled(ClickEnabled);
renderer.setBackgroundColor(Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setChartTitle("Simple data");
renderer.setAxesColor(Color.BLACK);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
mRenderer.addSeriesRenderer(renderer);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
return intent;
}
public void getData() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Counters_data");
query.getInBackground("lxFzCTeOcl", new GetCallback<ParseObject>() {
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
}
我有如何调用图表:
public void lineGraphHandler(View view) {
LineGraph line = new LineGraph();
line.getData();
Intent lineIntent = line.getIntent(this);
startActivity(lineIntent);
}
和XML部分:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/counters"
android:onClick="lineGraphHandler"
android:text="Charts"
android:id="@+id/charts"/>
有我的logcat:
03-26 08:42:13.096 1229-1229 / com.example.tst D / dalvikvm:延迟启用 CheckJNI 03-26 08:42:13.487 1229-1229 / com.example.tst D / libEGL:已加载 /system/lib/egl/libEGL_genymotion.so 03-26 08:42:13.491 1229-1229 / com.example.tst D /:HostConnection :: get()新主机 建立连接0xb94f4270,tid 1229 03-26 08:42:13.551 1229-1229 / com.example.tst D / libEGL:已加载 /system/lib/egl/libGLESv1_CM_genymotion.so 03-26 08:42:13.551 1229-1229 / com.example.tst D / libEGL:已加载 /system/lib/egl/libGLESv2_genymotion.so 03-26 08:42:14.035 1229-1229 / com.example.tst W / EGL_genymotion:eglSurfaceAttrib not not not 03-26 08:42:14.039 1229-1229 / com.example.tst E / OpenGLRenderer:从GradienCache 03-26获取MAX_TEXTURE_SIZE 08:42:14.043 1229-1229 / com.example.tst E / OpenGLRenderer: MAX_TEXTURE_SIZE:4096 03-26 08:42:14.055 1229-1229 / com.example.tst E / OpenGLRenderer:从中获取MAX_TEXTURE_SIZE Caches :: initConstraints()03-26 08:42:14.063 1229-1229 / com.example.tst E / OpenGLRenderer:MAX_TEXTURE_SIZE:4096 03-26 08:42:14.063 1229-1229 / com.example.tst D / OpenGLRenderer:启用调试模式0 03-26 08:42:50.327 1229-1229 / com.example.tst D / dalvikvm:GC_FOR_ALLOC 释放200K,8%免费2975K / 3228K,暂停10ms,总计13ms 03-26 08:42:51.675 1229-1229 / com.example.tst D / dalvikvm:GC_FOR_ALLOC已释放 431K,14%免费3056K / 3540K,暂停22ms,总计28ms 03-26 08:42:52.043 1229-1229 / com.example.tst W / EGL_genymotion:eglSurfaceAttrib not not not 实施03-26 08:42:53.543 1229-1229 / com.example.tst I / Choreographer:跳过89帧!该应用程序可能也在做 在其主要线程上做了很多工作。 03-26 08:43:01.747 1229-1229 / com.example.tst D / AndroidRuntime:关闭VM 03-26 08:43:01.747 1229-1229 / com.example.tst W / dalvikvm:threadid = 1:thread 退出未捕获的异常(组= 0xa4d8fb20)03-26 08:43:01.767 1229-1229 / com.example.tst E / AndroidRuntime:FATAL EXCEPTION:main 进程:com.example.tst,PID:1229 java.lang.IllegalStateException: 无法执行活动的方法 android.view.View $ 1.onClick(View.java:3823)at android.view.View.performClick(View.java:4438)at android.view.View $ PerformClick.run(View.java:18422)at android.os.Handler.handleCallback(Handler.java:733)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5017)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)at dalvik.system.NativeStart.main(Native方法)引起: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at android.view.View $ 1.onClick(View.java:3818)at android.view.View.performClick(View.java:4438)at android.view.View $ PerformClick.run(View.java:18422)at android.os.Handler.handleCallback(Handler.java:733)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5017)
在java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)引起: java.lang.NullPointerException at com.example.tst.LineGraph.getIntent(LineGraph.java:36)at com.example.tst.MainActivity.lineGraphHandler(MainActivity.java:44)
在java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at android.view.View $ 1.onClick(View.java:3818)at android.view.View.performClick(View.java:4438)at android.view.View $ PerformClick.run(View.java:18422)at android.os.Handler.handleCallback(Handler.java:733)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5017)
在java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
在dalvik.system.NativeStart.main(Native Method)03-26 08:43:04.507 1229-1229 / com.example.tst I / Process:发送信号。 PID:1229 SIG:9
我不明白问题所在。我的应用程序启动但是当我按下&#34; chart&#34;时立即崩溃按钮。是问题的数据类型还是因为我误解了什么? 提前谢谢。
我试过这样但仍然崩溃了:
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
ArrayList<Integer> y = dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
}
}
答案 0 :(得分:1)
您的getData()
异步检索数据。致电dataArray
时,getIntent()
不会立即初始化。
等待异步操作完成后再使用那里的数据。例如,调用需要done()
回调数据的代码。