LZ手頭一個Android程序有兩個activity,其中副activity主要就是畫個圖,且畫圖需要使用主activity中的一個數組的數據,我明明是先把有關數值填進了數組,完成了數組的處理之後才用intent開啟的第二個activty,但實際上副activity使用的還是數組在未處理前的全零狀態進行的與畫圖有關的運算,代碼如下
主activity的聲明
``` private LayoutInflater layoutInflater;
private View activity_main;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layoutInflater = LayoutInflater.from(this);
activity_main = layoutInflater.inflate(R.layout.activity_main, null);
setContentView(activity_main);
副activity
```protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View
{
//effect 為路徑效果(折線,曲線等)
PathEffect effect= new PathEffect();
private int[] colors;
private Paint paint;
private Path path;
private int widthScr;
private int heightScr;
private MainActivity mainAc;
public MyView(Context context){
super(context);
mainAc=new MainActivity();
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{
xAxis = 0;
yAxis = 0;
}
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw (Canvas canvas){
canvas.drawColor(Color.BLACK);//畫布背景為黑色
effect=new CornerPathEffect(10);//選擇曲線效果
paint.setPathEffect(effect);
paint.setColor(Color.WHITE);
canvas.drawPath(path, paint);
invalidate();
}
}
兩activity之間切換,MyPaint就是副activity的名字
``` Intent intent = new Intent(this,MyPaint.class);
startActivity(intent);
LZ乃Android入門菜雞,求問這bug是為啥?該怎麼改?求大神指點
mainAc=new MainActivity();這算什麼呀,建議去看看基礎,android開發沒java基礎也不行。
Intent intent = new Intent(this,MyPaint.class);
startActivity(intent);
數組在切換的時候傳遞
Bundle map = new Bundle();
map.putSerializable("alistdata", new int[]{15,2,12,2});
map.putSerializable("ad",new String[]{"5","6"});
intent.putExtra("sd", map);
對應的接收:
(int[])this.getIntent().getBundleExtra("sd").getSerializable("alistdata")
(String[])this.getIntent().getBundleExtra("sd").getSerializable("ad")