我正在尝试在android中绘制一条画布线,但到目前为止的每次尝试都导致“不幸的是[app-name]已经停止。你能告诉我我做错了什么吗?
这是我的主要内容:
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
这是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
,这是XML文件:
<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>
非常感谢任何帮助!
答案 0 :(得分:0)
没有log cat很难说,但我认为这可能是你的问题。 而不是:
<View
android:id="@+id/doodleView"
android:layout_width="match_parent"
android:layout_height="100dp"
/>
尝试:
<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.