android绘图不起作用,应用程序不断被杀死

android绘图不起作用,应用程序不断被杀死,android,canvas,drawing,draw,ondraw,Android,Canvas,Drawing,Draw,Ondraw,我正试图在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 exten

我正试图在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
 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
 <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>
这是涂鸦类视图:

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
 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
 <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>
这是XML文件:

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
 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
 <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>


任何帮助都将不胜感激!

没有日志猫很难说,但我认为这可能是你的问题。 而不是:

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

跟踪在崩溃情况下总是有用的:)如果没有logcat的堆栈跟踪,没有人能帮上忙。好吧,我可能和他们编程一样新,所以请原谅我的noob问题:我从哪里获得跟踪?logcat是我可以下载的应用程序吗?感谢一百万Tobiel!!在经历了三天轻微到中度的挫折之后,它现在可以工作了:D我仍然会阅读de尽管我很想学习,但仍然需要培训…再次感谢;)日志猫:如果您使用eclipse do窗口菜单,请使用日志猫显示视图->其他->Android->日志猫…另请参见