我有一个应用程序可以实时接收来自范围传感器的消息,并且我想用接收到的值更新图表。
我决定使用库GraphicalView
中的AChartEngine
。
我想在每次收到消息时更新Graphicalview
,然后使用BitMap
函数将其转换为toBitmap()
,然后在画布上绘制位图。
我正在尝试查看是否可以执行此操作(因此,我尚未使用消息,而是仅尝试从图形视图绘制位图),但是在运行代码时得到了Nullpointer exception
。这是我的代码:
public class DrawChartView extends View {
Paint paint = new Paint();
Rect rect = new Rect(130, 130, 80, 80);
Context context;
XYSeries currentDataset = new XYSeries("range data series");
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYSeriesRenderer currentRenderer = new XYSeriesRenderer();
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
LineChart lc;
GraphicalView gv;
Bitmap bm;
public DrawChartView(Context context_) {
super(context_);
context = context_;
//imposto i parametri del renderer
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(10);
mRenderer.addSeriesRenderer(currentRenderer);
//imposto il dataset
currentDataset.add(1,1);
currentDataset.add(2,2);
currentDataset.add(3,3);
mDataset.addSeries(currentDataset);
//inizializzo il line chart
lc = new LineChart(mDataset, mRenderer);
//inizializzo la grahical view
gv = new GraphicalView(context, lc);
bm=gv.toBitmap();
}
public DrawChartView(Context context_, AttributeSet attrs) {
super(context_, attrs);
// TODO Auto-generated constructor stub
}
public DrawChartView(Context context_, AttributeSet attrs, int defStyle) {
super(context_, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
//canvas.drawRect(rect, paint);
// canvas.drawBitmap(bm,new Rect(0,0,100,100),rect,null);
}
public void setRect(int h, int b)
{
rect.set(130,130,30+b,30+h);
}
public void setChart(XYSeries xys)
{
mDataset.addSeries(xys);
lc = new LineChart(mDataset, mRenderer);
gv= new GraphicalView(context, lc);
bm = gv.toBitmap();
}
活动:
public class FragActivity extends Activity {
Canvas canvas = new Canvas();
XYSeries currentSeries = new XYSeries("range data series1");
DrawChartView dcv/* = new DrawChartView(this)*/;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frag);
dcv = (DrawChartView)findViewById(R.id.SurfaceView01);
dcv.onDraw(canvas);
currentSeries.add(11, 11);
dcv.setChart(currentSeries);
dcv.onDraw(canvas);
}
}
和布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.cami.robot_chooser.DrawChartView
android:id="@+id/SurfaceView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<TextView
android:id="@+id/show_message1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:layout_weight="1"
/>
</LinearLayout>
您能帮助找出问题所在以及如何解决该问题吗?
LogCat说:
10-22 12:52:06.115: I/Choreographer(3061): Skipped 71 frames! The application may be doing too much work on its main thread.
10-22 12:52:06.155: D/gralloc_goldfish(3061): Emulator without GPU emulation detected.
10-22 12:52:08.406: I/Choreographer(3061): Skipped 34 frames! The application may be doing too much work on its main thread.
10-22 12:52:08.581: D/AndroidRuntime(3061): Shutting down VM
10-22 12:52:08.581: W/dalvikvm(3061): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-22 12:52:08.675: E/AndroidRuntime(3061): FATAL EXCEPTION: main
10-22 12:52:08.675: E/AndroidRuntime(3061): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cami.robot_chooser/com.cami.robot_chooser.FragActivity}: java.lang.NullPointerException
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.os.Looper.loop(Looper.java:137)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-22 12:52:08.675: E/AndroidRuntime(3061): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:52:08.675: E/AndroidRuntime(3061): at java.lang.reflect.Method.invoke(Method.java:511)
10-22 12:52:08.675: E/AndroidRuntime(3061): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-22 12:52:08.675: E/AndroidRuntime(3061): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 12:52:08.675: E/AndroidRuntime(3061): at dalvik.system.NativeStart.main(Native Method)
10-22 12:52:08.675: E/AndroidRuntime(3061): Caused by: java.lang.NullPointerException
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.view.View.<init>(View.java:3228)
10-22 12:52:08.675: E/AndroidRuntime(3061): at com.cami.robot_chooser.DrawChartView.<init>(DrawChartView.java:36)
10-22 12:52:08.675: E/AndroidRuntime(3061): at com.cami.robot_chooser.FragActivity.<init>(FragActivity.java:41)
10-22 12:52:08.675: E/AndroidRuntime(3061): at java.lang.Class.newInstanceImpl(Native Method)
10-22 12:52:08.675: E/AndroidRuntime(3061): at java.lang.Class.newInstance(Class.java:1319)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-22 12:52:08.675: E/AndroidRuntime(3061): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-22 12:52:08.675: E/AndroidRuntime(3061): ... 11 more
10-22 12:52:13.545: I/Process(3061): Sending signal. PID: 3061 SIG: 9
最佳答案
首先,请确保已添加AndroidManifest.xml
中的将文件保存到外部存储中的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果您遇到了这种情况,但仍然无法正常工作,请查看有关此主题here和here的讨论。