LZ是Android入門菜雞,求大神拯救,我在程序的一開始用xml布局文件寫了一個按鈕,然後這個xml是以view的形式顯示的,我的設想是一點擊按鈕,程序就在當前view中開啟另一個畫圖的view。
最開始是這樣聲明的
```public float xAxis = 0;
public float yAxis = 0;
private Button start;
private LayoutInflater layIn;
private View paint_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layIn = LayoutInflater.from(this);
paint_layout = layIn.inflate(R.layout.paint_layout, null);
setContentView(paint_layout);
//setContentView(new MyView(this));
start = (Button) findViewById(R.id.start);
start. setOnClickListener(MyPaint.this);
start.setVisibility(View.VISIBLE);
然後點擊事件是這樣的
```public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
setContentView(new MyView(this));
break;
default:
break;
}
}
Myview是這樣的
``` public MyView(Context context){
super(context);
Display mDisplay = getWindowManager().getDefaultDisplay();
widthScr = mDisplay.getWidth();
heightScr= mDisplay.getHeight();
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
waitMain();
path=new Path();
path.moveTo(xAxis, yAxis);
for (int j=0; j<10;j++){
path.lineTo( j*75 + xAxis , j*75 + yAxis);
}
}
private void waitMain() {
// TODO Auto-generated method stub
if(mainAc.startPaint[0] == 1){
xAxis = widthScr/2;
yAxis = heightScr/2;
}else{
waitMain();
}
}
protected void onDraw (Canvas canvas){
canvas.drawColor(Color.BLACK);//畫布背景為黑色
effect=new CornerPathEffect(10);//選擇曲線效果
//canvas.translate(8, 8);
paint.setPathEffect(effect);
paint.setColor(Color.WHITE);
canvas.drawPath(path, paint);
//canvas.translate(0, 60);
invalidate();
}
}
現在的情況是一點擊按鈕程序就卡死直接強退,求問大神們這是為什麼啊?
Activity的setContentView(new MyView(this));方法是不能重復調用的.
paint_layout = layIn.inflate(R.layout.paint_layout, null);
你可以用新的view 替換 paint_layout 的 view
學習一下 android ViewGroup