Android 画布绘制方法
我是android编程新手,我正在尝试在屏幕外创建一个绿色圆圈图像。我尝试使用画布绘制,但始终显示错误:Android 画布绘制方法,android,canvas,Android,Canvas,我是android编程新手,我正在尝试在屏幕外创建一个绿色圆圈图像。我尝试使用画布绘制,但始终显示错误: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Paint.setColor(int)' on a null object reference at com.example.jordanong09.handeyegame
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Paint.setColor(int)' on a null object reference
at com.example.jordanong09.handeyegame.GameActivity.onDraw(GameActivity.java:61)
at com.example.jordanong09.handeyegame.GameActivity.onCreate(GameActivity.java:47)
以下是我的课程代码:
public class GameActivity extends Activity {
TextView textViewTime, textViewScore;
int timeLeft,score;
float xcoord,ycoord,radius = 100;
Paint paint;
Canvas canvas1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
String timeData = getIntent().getExtras().getString("timeDuration");
if(timeData == "30sec"){
timeLeft = 30;
}
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewScore = (TextView)findViewById(R.id.textViewScore);
textViewTime.setText(timeData);
final CounterClass timer = new CounterClass (30000, 1000);
timer.start();
onDraw(canvas1);
}
protected void onDraw (Canvas canvas)
{
Display display = getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int width = screenSize.x;
int height = screenSize.y;
Random random = new Random();
xcoord = random.nextFloat() * width;
ycoord = random.nextFloat() * height;
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(xcoord, ycoord, radius, paint);
}
我真的不知道它有什么问题,因为我一直在网上寻找解决方案。希望我能听到一些建议。谢谢绘制对象未正确初始化。你应该打电话
paint = new Paint();
打电话之前
paint.setColor();
否则,paint为null,您不能对null调用方法。您应该创建一个自定义视图,并将其作为子视图添加到一个视图组中。下面是一个示例代码。希望这会有所帮助
/**
* Created by sanjeet on 2/2/16.
*/
public class GameActivity extends AppCompatActivity {
TextView textViewTime, textViewScore;
int timeLeft, score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewScore = (TextView)findViewById(R.id.textViewScore);
/*Create a ViewGroup in your layout to contain this custom GameView instance */
LinearLayout gameViewContainer = (LinearLayout)findViewById(R.id.custom_view_container);
gameViewContainer.addView(new GameView(this));
}
class GameView extends View {
Paint paint;
int width;
int height;
private float radius=100;
Random random;
public GameView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
Display display = getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
width = screenSize.x;
height = screenSize.y;
random = new Random();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float xcoord = random.nextFloat() * width;
float ycoord = random.nextFloat() * height;
canvas.drawCircle(xcoord, ycoord, radius, paint);
}
}
}
我草签了油漆。但我得到了这个错误:尝试在空对象引用上调用虚拟方法“void android.graphics.Canvas.drawCircle(float,float,float,android.graphics.Paint)”,您也需要实例化Canvas对象。正如Sanjeet所说,Canvas需要初始化为新的Canvas()对象。添加自定义视图是什么意思?在我的布局中创建包含此自定义gameview实例的视图组,我不理解该部分