二、The specified child already has a parent. You must call removeView() on the child's parent first....

本文介绍了一种在Android应用中处理不同屏幕方向布局切换的方法,通过使用ActivityManager来管理视图,并解决了在屏幕旋转过程中出现的View重复添加错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我写了一个应用程序,需要竖屏和横屏显示不同的布局

if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { setNormalHorizontalView(); } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { setNormalVerticalView(); }在竖屏时会使用ActivityManager进行视图切换

mFrameLayout.removeAllViews(); switch(mPresentActivity){ case 0: mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,GinwavePlayer.class)).getDecorView())); break; case 1: mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,Album.class)).getDecorView())); break; case 2: mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,GinwaveMusic.class)).getDecorView())); break; case 3: mFrameLayout.addView((mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,PlayList.class)).getDecorView())); break; }从竖屏切换到横屏然后再切换到竖屏时会出现The specified child already has a parent. You must call removeView() on the child's parent first.的bug,我按照提示已经添加了mFameLayout.removeAllViews()函数,但还是会出现这个问题,我想可能是由于在竖屏时已经执行过mLocalActivityManager.startActivity("", new Intent(GinwaveIMusic.this,GinwavePlayer.class)).getDecorView()方法,从横屏转换到竖屏后又会执行这个方法,而第一次切换后并没有进行对这个View进行释放,所以我在由竖屏切换到横屏时对这个View进行释放,我在onConfiguration中增加了以下代码后就没有问题了

if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { if(mFrameLayout != null){ mFrameLayout.removeAllViews(); } setNormalHorizontalView(); }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值