我想在单击菜单CircleMyView.circle()
时调用方法,但是当我单击菜单时它会抛出一个,这意味着该对象没有按我预期的那样初始化。NullPointerException
这是我的代码。我哪里错了?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,"Circle");
menu.add(0,1,2,"Rect");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case 1:
MyView.circle();
break;
}
return super.onOptionsItemSelected(item);
}
public static class MyView extends View {
private Context context;
static Canvas can=null;
public MyView(Context c) {
super(c);
context=c;
}
@Override
protected void onDraw(Canvas canvas) {
mPaint=new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
//circle(canvas);
}
public static void circle() {
// TODO Auto-generated method stub
can.drawCircle(50, 50, 50, mPaint);
}
}
}