package com.yunlei.android_1;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
MyView myview=new MyView(this);
this.setContentView(myview);
}
}
package com.yunlei.android_1;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyView extends View{
Paint paint;//定义画笔
public MyView(Context context) {
super(context);
// TODO 自动生成的构造函数存根
paint.setColor(Color.WHITE);//设置画笔颜色
paint.setTextSize(20);//设置字体大小
paint.setAntiAlias(true);//设置抗锯齿
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.GRAY);
canvas.drawRect(10, 10, 100, 100, paint);
canvas.drawText("jfalkfjjdfkdjfk", 60,70, paint);
}
}
------解决方案--------------------
Paint paint;//定义画笔
=>
Paint paint=new Paint();//定义画笔
------解决方案--------------------
空指针,骚年。目测是paint 没有初始化,问题太简单了。大家都不乐意答