I'm trying to draw a line to a canvas in android, but every attempt so far has led to "unfortunately [app-name] has stopped. Could you tell me what I'm doing wrong?
this is my main:
package com.example.mapz;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
private DoodleView doodleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get reference to doodleview
doodleView = (DoodleView) findViewById(R.id.doodleView);
}//end oncreate
}//end main activity
This is the class DoodleView:
package com.example.mapz;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DoodleView extends View
{
private Bitmap bitmap;
private Canvas bitmapCanvas;
private Paint paintScreen;
private Paint paintLine;
//constructor
public DoodleView(Context context, AttributeSet attrs)
{
super(context,attrs);//pass context to views constructor
paintScreen = new Paint();
paintScreen.setColor(Color.WHITE);
paintLine =new Paint();
paintLine.setAntiAlias(true);
paintLine.setColor(Color.BLACK);
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(5);
paintLine.setStrokeCap(Paint.Cap.ROUND);
}// end doodleview constructor
//on size changed creates bitmap and canvas after the screen is initialized
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
bitmap = Bitmap.createBitmap(getWidth(), getHeight(),Bitmap.Config.ARGB_8888);//indicates aRGB 256 value format
bitmapCanvas = new Canvas(bitmap);
bitmap.eraseColor(Color.WHITE);
}//end on size changed
@Override
protected void onDraw (Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paintScreen);
canvas.drawLine(5, 5, 25, 25, paintLine);
}//end on draw
}//end doodleview
and this the XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
tools:context=".MainActivity" >
<View
android:id="@+id/doodleView"
android:layout_width="match_parent"
android:layout_height="100dp"
/>
</RelativeLayout>
Any help will be much appreciated!