Error swapping / adding an XML Fragment

I have been struggling with the Android 3.0 SDK's fragments concept for a few days now, and thought I'd try StackOverflow....

I have a simple (proof on concept) app:

Here is my main layout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="200dp"> <fragment android:name="com.owentech.simplefragmentswap.staticfragment" android:id="@+id/fragment1" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="fill_parent"/> </RelativeLayout> <FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_above="@+id/relativeLayout1"></FrameLayout> </RelativeLayout>

I display one fragment (staticfragment) directly in the XML, this works fine.

With the framelayout I wanted to add an XML fragment (fragment1) on create.

Here is fragment1:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_width="match_parent" android:text="Fragment 1"></Button> </RelativeLayout>

Here is my mainactivity:

import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; public class mainActivity extends FragmentActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create new fragment and transaction Fragment newFragment = new fragment1(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack //transaction.replace(R.id.frameLayout1, newFragment); transaction.add(R.id.frameLayout1, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } }

This causes a force close on start.

Here is the logcat:

05-18 09:49:23.073: INFO/ActivityManager(71): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.owentech.simplefragmentswap/.mainActivity } 05-18 09:49:23.313: INFO/ActivityManager(71): Start proc com.owentech.simplefragmentswap for activity com.owentech.simplefragmentswap/.mainActivity: pid=451 uid=10036 gids={1015} 05-18 09:49:23.324: DEBUG/AndroidRuntime(445): Shutting down VM 05-18 09:49:23.333: DEBUG/dalvikvm(445): Debugger has detached; object registry had 1 entries 05-18 09:49:23.404: INFO/AndroidRuntime(445): NOTE: attach of thread 'Binder Thread #3' failed 05-18 09:49:24.433: DEBUG/AndroidRuntime(451): Shutting down VM 05-18 09:49:24.433: WARN/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): FATAL EXCEPTION: main **05-18 09:49:24.473: ERROR/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.owentech.simplefragmentswap/com.owentech.simplefragmentswap.mainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.** 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:521) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1865) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1822) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1802) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:916) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1587) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:500) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.Activity.performStart(Activity.java:3781) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) 05-18 09:49:24.473: ERROR/AndroidRuntime(451): ... 11 more 05-18 09:49:24.494: WARN/ActivityManager(71): Force finishing activity com.owentech.simplefragmentswap/.mainActivity

Does anybody know what I am doing wrong, as I have taken the code directly from developer.android.com

Thanks

-------------Problems Reply------------

Sorry, found the answer. When I inflated the view in my fragment1.java it was attached to root. changed the last flag to false, now ok.

Category:android Views:1 Time:2011-05-18

Related post

  • Entity Framework 4 mapping fragment error when adding new entity scalar 2010-06-08

    I have an Entity Framework 4 model-first design. I create a first draft of my model in the designer and all was well. I compiled, generated database, etc. Later on I tried to add a string scalar (Nullable = true) to one of my existing entities and I

  • XML fragment with multiple roots: suppressing VS error 2010-05-26

    In Visual Studio, I have an XML file (with .xml extension) which contains an XML fragment that I use in my program: <tag1> data ... </tag1> <tag2> data ... </tag2> Visual Studio shows me an error in the error list: "XML docume

  • Error while trying to canonize XML fragments in XML::LibXML 2012-02-15

    I am trying to canonize a XML fragment to avoid printing of self closing nodes in the resultant XML. ( i want node <test /> to be streamed as <test></test> due to constraints at the consuming system ). I tried to use toStringC14N()

  • Parse file containing XML Fragments in Java 2010-07-12

    I inherited an "XML" license file containing no root element, but rather two XML fragments (<XmlCreated> and <Product>) so when I try to parse the file, I (expectantly) get an error about a document that is not-well-formed. I need to get

  • How to deserialize an Xml Fragment using the XML Reader 2009-03-16

    I am trying to deserialize a Xml Fragment. I am nearly there but it throws an error where by it does not expect the first element. An example of the XML in the stream is as follows: <Project xmlns="http://schemas.datacontract.org/2004/07/Swissmod.

  • Convert String XML fragment to Document Node in Java 2009-04-08

    In Java how can you convert a String that represents a fragment of XML for insertion into an XML document? e.g. String newNode = "<node>value</node>"; // Convert this to XML Then insert this node into an org.w3c.dom.Document as the child

  • Using XPath with HTML or XML fragment? 2010-09-28

    I am new to Nokogiri and XPath, and I am trying to access all comments in a HTML or XML fragment. The XPaths .//comment() and //comment() work when I am not using the fragment function, but they do not find anything with a fragment. With a tag instea

  • Store XML fragment in Project Resources 2010-11-12

    I'm trying to follow a tutorial on openXML here. I don't follow the bit where he talks about storing an XML fragment in project resources. He can then obtain a copy of the XML fragment easily with String content = Properties.Resources.XmlContentForEm

  • Error 3004: Problem in mapping fragment starting at line 2011-05-19

    I have this error when I build my Web Service: Error 3004: Problem in mapping fragment starting at line 323: No mapping specified for properties JE_TRN_HS.JE_HDR_HSJE_HDR_KEY. Entity is type [TESTCPModel.JE_TRN_HS This is what happend. I created this

  • Inserting XML Fragment after last specific node/element 2011-05-31

    I want to add an XML fragment to the last element to an XML document and I having problems i.e. the error I get is: "The reference node is not a child of this node". So my existing XML document looks like this: <MAP> <LAYER name ="My first L

  • Python lxml using xi:include with multiple Xml fragments 2012-01-11

    I am developing a simple xml logfile class using lxml in Python. My approach so far has been to use two files. A well-formed XML file which includes a second file which is a XML fragment. I am using an xi:include element. This way XML fragment can be

  • I'm trying to parse an XML fragment with Linq to XML 2012-02-03

    I trying to parse the "message" element out of the following XML fragment using Linq to XML but for some reason it isn't working: <error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <code/> <message xml:lang=

  • Setting DataGridView.DefaultCellStyle.NullValue to null at designtime raises error at adding rows runtime 2008-09-07

    In Visual Studio 2008 add a new DataGridView to a form Edit Columns Add a a new DataGridViewImageColumn Open the CellStyle Builder of this column (DefaultCellStyle property) Change the NullValue from System.Drawing.Bitmap to null Try to add a new Row

  • How to merge jsp pre-compiled web.xml fragment with main web.xml using Ant 2008-10-02

    We have the usual web.xml for our web application which includes some jsp and jsp tag files. I want to switch to using pre-compiled jsp's. I have the pre-compilation happening in the build ok, and it generates the web.xml fragment and now I want to m

  • how to create xml from xml fragments 2009-02-11

    I am using SQL Server 2005 and I have a XML Data Type column that stores xml fragments like so in one big table: row 1 ..... <Order date='2009-02-11' customerID='4' /> row 2...... <OrderItem OrderID='6' ItemID='477' quantity='1' /> I woul

  • How can I get an XML fragment as a string from an XElement? 2009-03-06

    I have an XElement and inside that element I have another XML fragment. How do I retrieve the XML? If I try the following, I get only the value: string str = "<Root><Node1>value1</Node1><Node2></Node2></Root>"; XEl

  • DB function reports no errors when adding new article in Joomla! 2009-08-06

    Possible Duplicate: 500 - An error has occurred! DB function reports no errors when adding new article in Joomla! I have an article that I want to publish on my Joomla! site. Every time I click apply or save. I get error 500 - An error has occurred!

  • HTML Entity problems using Nokogiri::XML.fragment 2009-09-09

    it seems that all entities are killed using tags = "<p>test umlauts &ouml;</p>" Nokogiri::XML.fragment(tags) Result: <p>test umlauts </p> The above method calls Nokogiri::XML::DocumentFragment.parse(tags) and that methods

  • JBoss Error in ejb-jar.xml for Message Driven Bean TextMDB: expected one res-ref-name tag 2009-11-03

    I've copied and pasted the example ejb-jar.xml and jboss.xml file from http://docs.jboss.org/jbossas/jboss4guide/r4/html/ch6.chapt.html#ch6.mdbejbjar.ex and I'm receiving the following error when JBoss attempts to deploy it: ERROR [ScannerThread] dep

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.140 (s). 11 q(s)