2012-05-01 25 views
0

我为android创建了一个桌面时钟。为此,我创建了一个RelativeLayout来显示小时,分钟等...和一个菜单,以显示关于和退出...选项,一切正常,但是当我点击关于选项时,问题出现:它强制关闭和logcat中显示了这个:昨天我为孩子的父母调用removeView()

E/AndroidRuntime(14751): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iamaner.T2Speech/com.iamaner.T2Speech.FrmAbout}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

所以我希望你能帮助我... 伊夫听到这个代码,但我不知道放在哪里,以及如何,我认为这将是好的,但它似乎没有

final RelativeLayout fondo = (RelativeLayout)findViewById(R.id.your_layout);((RelativeLayout)fondo.getParent()).removeView(fondo); //scrollView.removeView(scrollChildLayout);

这里是清单文件,我认为它的这个问题,我不知道:

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".TimeToSpeechActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" 
     android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".FrmAbout" /> 
</application> 

这里的FrmAbout代码:

public class FrmAbout extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.frmabout); 

    TextView txtVersion = (TextView)findViewById(R.id.Txtversion); 
    TextView txtWarning = (TextView)findViewById(R.id.Txtwarning); 
    TextView txtInstructions = (TextView)findViewById(R.id.Txtinstructions); 
    TextView txtContact = (TextView)findViewById(R.id.Txtcontact); 

    setContentView(txtVersion); 
    setContentView(txtWarning); 
    setContentView(txtInstructions); 
    setContentView(txtContact); 

    } 
    } 

好了,解决了这个问题....这个问题是的setContentView()方法,删除了它,现在它的工作原理

+1

试试这个:((ViewGroup)scrollChildLayout.getParent())。removeallViews();它在这个答案中建议。 http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first –

回答

0

从你的代码我不明白为什么你在调用关于活动之前删除所有视图?这是你的任何要求,因为要开始新的活动,所有的视图不需要删除..你可以直接调用startActivity(intent)而不删除当前活动的视图。

请张贴日志以帮助理解问题。

+0

好的,编辑并添加logcat错误..我做了这个因为logcat说我必须在启动FrmAbout之前调用removeView()... – BamsBamx

0

此问题已被询问,下次尝试使用搜索! Take a look at the answer here,我在我的代码中使用它,它效果很好。

+0

谢谢我搜索它找到你发布的内容,但我不知道我必须把那个....也许在第一个代码?你认为这会好吗? '最终RelativeLayout fondo =(RelativeLayout)findViewById(R.id.your_layout);','((ViewGroup)fondo.getParent())。removeView(fondo);' – BamsBamx

相关问题