Question

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!

Was it helpful?

Solution

Without log cat is hard to say but I think this can be your issue. Instead of:

<View
    android:id="@+id/doodleView"
android:layout_width="match_parent"
android:layout_height="100dp"
/>

try:

<com.example.mapz.DoodleView  
android:id="@+id/doodleView"
android:layout_width="match_parent"
android:layout_height="100dp"
 />

DOCS: http://developer.android.com/training/custom-views/create-view.html

Notice the name of the XML tag that adds the custom view to the layout. It is the
fully qualified name of the custom view class. If your view class is an inner class, 
you must further qualify it with the name of the view's outer class. further.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top