距離センサーからリアルタイムでメッセージを受信するアプリがあり、受信した値でチャートを更新したいと考えています。GraphicalView
ライブラリの a を使用することにしました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