在孩子的父母上调用removeView()

时间:2012-05-01 11:07:22

标签: android forceclose

昨天我为android创建了一个Desk时钟。为此,我创建了一个RelativeLayout来显示小时,分钟等...以及一个菜单来显示选项关于和退出......好吧,一切正常,但是当我点击关于选项时出现问题:它强制关闭和LogCat显示了这个:

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()方法,删除它,现在它可以正常工作

2 个答案:

答案 0 :(得分:0)

之前已经问过这个问题,下次尝试使用搜索! Take a look at the answer here,我在我的代码中使用它,效果很好。

答案 1 :(得分:0)

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

请发布日志以帮助您了解问题。

相关问题