作者:特异型爷们喵 | 来源:互联网 | 2023-09-05 12:02
用achartengine画折线图,想让X轴显示我设置的文字,怎么一直报空指针错误啊,代码和errorlog如下我把红色那行也就是设置X轴labletext去掉就可以正常运行了。同样的方法用在柱状
用achartengine画折线图,想让X轴显示我设置的文字,怎么一直报空指针错误啊,代码和error log如下
我把红色那行也就是设置X轴labletext去掉就可以正常运行了。同样的方法用在柱状图里就没有错,也不知道为什么啊!!!!
代码:
package com.myandroid.graduationDesign;
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
/**
* Temperature demo range chart.
*/
public class LineChart extends AbstractDemoChart {
private double value[]=new double[6];
private String time[]=new String[6];
private String title= "消费结构分析图";
public LineChart( String time[],double[] values) {
for (int i=0;i
time[i]=time[i];
value[i] = values[i];
}
}
public String getName() {
return title;
}
/**
* Returns the chart description.
*
* @return the chart description
*/
public String getDesc() {
return "The monthly temperature (vertical range chart)";
}
/**
* Executes the chart demo.
*
* @param context the context
* @return the built intent
*/
public Intent execute(Context context) {
String[] titles = new String[] { "月消费趋势图" };//图例
List x = new ArrayList();
x.add(new double[] { 1, 2, 3, 4, 5, 6 });//点的X坐标
List y = new ArrayList();
y.add(value);//点的y坐标
int[] colors = new int[] { Color.BLUE};//每个序列的颜色设置
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };//每个序列中点的形状设置
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);//调用AbstractDemoChart中的方法设置renderer.
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);//设置图上的点为实心
}
for(int i=0;i<6;i++){
renderer.addXTextLabel(i+1,time[i]);
}
setChartSettings(renderer, title, "最近6个月", "消费金额", 0, 7, 0, 500,
Color.LTGRAY, Color.LTGRAY);//调用AbstractDemoChart中的方法设置图表的renderer属性.
renderer.setXLabels(7);//设置x轴显示6个点,根据setChartSettings的最大值和最小值自动计算点的间隔
renderer.setYLabels(10);//设置y轴显示10个点,根据setChartSettings的最大值和最小值自动计算点的间隔
renderer.setShowGrid(true);//是否显示网格
renderer.setXLabelsAlign(Align.RIGHT);//刻度线与刻度标注之间的相对位置关系
renderer.setYLabelsAlign(Align.CENTER);//刻度线与刻度标注之间的相对位置关系
renderer.setZoomButtonsVisible(true);//是否显示放大缩小按钮
Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, y), renderer, title);//构建Intent
return intent;
}
}
error log:
04-24 01:28:45.313: E/AndroidRuntime(752): FATAL EXCEPTION: main
04-24 01:28:45.313: E/AndroidRuntime(752): java.lang.NullPointerException
04-24 01:28:45.313: E/AndroidRuntime(752): at org.achartengine.chart.AbstractChart.drawString(AbstractChart.java:157)
04-24 01:28:45.313: E/AndroidRuntime(752): at org.achartengine.chart.XYChart.drawText(XYChart.java:584)
04-24 01:28:45.313: E/AndroidRuntime(752): at org.achartengine.chart.XYChart.drawXTextLabels(XYChart.java:749)
04-24 01:28:45.313: E/AndroidRuntime(752): at org.achartengine.chart.XYChart.drawXLabels(XYChart.java:660)
04-24 01:28:45.313: E/AndroidRuntime(752): at org.achartengine.chart.XYChart.draw(XYChart.java:326)
04-24 01:28:45.313: E/AndroidRuntime(752): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:166)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.View.draw(View.java:10978)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.View.draw(View.java:10981)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.widget.FrameLayout.draw(FrameLayout.java:450)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.View.draw(View.java:10981)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.widget.FrameLayout.draw(FrameLayout.java:450)
04-24 01:28:45.313: E/AndroidRuntime(752): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.os.Looper.loop(Looper.java:137)
04-24 01:28:45.313: E/AndroidRuntime(752): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 01:28:45.313: E/AndroidRuntime(752): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:28:45.313: E/AndroidRuntime(752): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 01:28:45.313: E/AndroidRuntime(752): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 01:28:45.313: E/AndroidRuntime(752): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 01:28:45.313: E/AndroidRuntime(752): at dalvik.system.NativeStart.main(Native Method)
5 个解决方案