问题描述
- Android编程两activity间切换问题
-
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")
解决方案二:
http://blog.csdn.net/lsj19830812/article/details/7110028
解决方案三:
Activity之间的切换需要用Intent。
解决方案四:
楼主多看看文档,intent方法里可以绑定数据传给下一个act
时间: 2024-10-29 05:28:11