Duplicate ID with Fragment on PagerAdapter page - android

i´m facing a problem with the pageradapter and fragments.
i have a PagerAdapter and on every page i want to put a fragment. on the xml layout of the page, i use this code to add the fragment:
<fragment android:id="#+id/fragment1"
android:name="de.worldcup.android.ui.fragments.GroupTableFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
when i run the application, it crashes when it tries to instanciate the second page of the PagerAdapter (2nd time the instantiateItem method is called) at the line, when i try to inflate the xml layout of the page.
View v = inflater.inflate(R.layout.group_activity_item, null);
the error:
03-15 21:57:34.795: E/AndroidRuntime(6257): Caused by: java.lang.IllegalArgumentException: Binary XML file line #20: Duplicate id 0x7f050043, tag null, or parent id 0x0 with another fragment for de.worldcup.android.ui.fragments.GroupTableFragment
i looked up the id 0x7f050043 -> it is the #+id/fragment1 id.
any ideas how to fix this?
thanks :)

Facing the same problem myself I found this:
"Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically."
In the documentation. In short, create your fragment architecture dynamically to solve the problem.

I had the same problem. I don't think we can not have 2 fragments with the same id in a page.
What i do (but i think it's not the best solution, but it works):
Create one layout for each fragment with a different id.

Related

Android studio - new Activity with Fragment (XML files explanation)

I want to create a new Activity in Android Studio with MainActivity as a parent. Although, when I create a blank activity (ex. NewActivity) with a fragment added I get the two expected classes
(NewActivity and NewActivityFragment) but when I check the layout resources, I can't understand why there are 3 XML files auto-generated and what's their meaning?
What is the exact meaning of each XML-file generated ? The 3 XML files are the following : activity_new.xml, fragment_new.xml and content_new.xml
For example , if i want to add a TextView in the second activity, which XML file shall i modify?
When you create a New activity with a Fragment using the wizard in Android Studio, it will generate two src files :
NewActivity.java
NewActivityFragment.java
and three res files:
activity_new.xml
content_new.xml
fragment_new.xml
The details goes as below:
The activity class NewActivity.java inflates the layout activity_new.xml in onCreate() method as below.
setContentView(R.layout.activity_new);
This layout is a CoordinatorLayout and contains the Appbarlayout, FAB and the container for your main comonents.
activity_new.xml includes another layout using include tag.
content_new.xml is a fragment xml file and contains the attributes to define its layouts.
observer that content_new.xml has an attribute as below
tools:layout="#layout/fragment_new"
fragment_new.xml is the layout which gets inflated in NewActivityFragment onCreateView() method.
inflater.inflate(R.layout.fragment_new, container, false);
fragment_new.xml is the layout file that is the place where you go for adding the components to be shown in the fragment. So as per your requirement of adding a TextView in the fragment you need to add it in fragment_new.xml.
If you check activity_new.xml, you will see an <include> tag calling the content_new.xml.

Android: Multiple Fragments from same XML file

I have a view pager that has a page for each subject in an array (English, Maths, Science, etc). The fragments in the view pager are all created from the same XML file. These fragments then have fragments added inside of them.
As all the parents inflate from the same XML file, I can not specify which fragment I want to add the child fragment to. I was wondering if their was a way to do this - Narrow the scope of the id maybe??
I think you want to implement multiple fragments with the capability to manipulate them depending on user selections. There is Google link Fragments
Review these code on that web page:
The many samples of Fragment and FragmentTransaction
Look at method showDetails in class TitlesFragment, for example.
If you want a working sample, look at Working with Android Fragments. Snippet from the webpage:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<fragment
android:id="#+id/headFrag"
android:name="com.intertech.HeaderFrag"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/bodyFrag"
android:name="com.intertech.BodyFrag"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
And the code
...
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
// work here to change Activity fragments (add, remove, etc.).
fragmentTransaction.add (R.id.myFrame, myFrag);
fragmentTransaction.commit ();
From above, notice there are 2 fragments in the layout. And fragmentTransaction.add method should specify parent container in R.id form.
Someday I may want/need to implement this. So far I implemented show/hide Layouts instead (not regretting it), and they can have IDs for me to manipulate.
Have fun and keep us posted, Tommy Kwee.
First. I suggest you to try to avoid child fragment, if you really doesn't have very good reason to use them.
But I don't understand where is problem. I don't see any problem if more fragment use the same XML for the inflate. You can call setArguments() on all parent fragment and put here some Bundle which can be used to recognize which fragment it is inside of it by getArguments().

ViewStub Null exception inflate

I get an Error
07-04 01:49:56.325: E/AndroidRuntime(9693): java.lang.NullPointerException
while try to inflate ViewStub in Fragment after transaction done
sendMessageView = ((ViewStub) rootView
.findViewById(R.id.send_message_viewstub)).inflate();
Here are XML
<ViewStub
android:id="#+id/send_message_viewstub"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inflatedId="#+id/panel_import"
android:layout="#layout/fragment_send_message" />
Agree with Shawnic, that error line you are showing us doesn't tell much. I can tell you that using fragments is a little trickier than activities.
But with that said, I don't think you can use a fragment as the included layout?
What or where exactly is the Fragment within the context of your question?
Fragments have their own class files that take care of inflating a layout to be used as the Fragment.
Also I am concerned about this part you posted:
sendMessageView = ((ViewStub) rootView
.findViewById(R.id.send_message_viewstub)).inflate();
Now I have experience with this but it's been a while and I am not sure if this part of your code is correct?
This is how Android recommends you write it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
In Your Code, what kind of object is
sendMessageView
You may want to change your code to this first just to make sure, we don't know exactly why Android suggests we do it this way but we probably should.
If you're always going to be inflating the same layout in this ViewStub, you would be better served using an include tag.
<include layout="#layout/fragment_send_message />
include replaces itself with the specified layout at runtime and allows your layouts to be both modular and readable.

Get the desired view when dealing with fragments when the view's id has a duplicate name in both xml

I'm dealing now with fixed tabs and fragments. I have 2 tabs. But it happened that the 2 xml files are a copy and paste they differ slightly. I do not want to change the id name of each view there.
The first xml file name is :first.xml
the second is :second.xml
When initializing the views in my main activity extending the 2 fragments (inner classes), how can I determine the desired views if they have the same id name ?
For example:
TextView tv = (TextView)findViewById(R.id.textView1); //However, this exists in both first.xml and second.xml , how can I tell it to take it from second.xml and not from first.xml ?
Thank you!

Wrong layout hierarchy created from inflating

I have a custom component that actually wraps another component. Its layout is:
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/text_view_background" android:textCursorDrawable="#null"
android:textColor="#android:color/black" android:inputType="textNoSuggestions"
android:paddingLeft="7dp"/>
In the component's code I'm trying to inflate it:
inflate(context,R.layout.results_auto_complete,this);
resultsAutoComplete=(AutoCompleteTextView)getChildAt(0);
But I'm getting a ClassCastException and it says the first child is a RelativeLayout! I traced all the children of this relative layout and it's actually the layout of the widget whose configuration activity contains my custom component! When I tested the component with a simple test activity everything worked!
So why is it happening and what can I do about this?
Thanks.
If your AutoCompleteTextView is a standalone xml file (your code is the root xml tag) of results_auto_complete.xml. It is the result of the inflation, NO need to use getChildAt(i).
if <AutoCompleteTextView> is a child element in an XML file, use should assign it an Id: android:id="#+id/your_view_id". Then after the inflation, use:
this.findViewById(R.your_view_id);
where this is current activity which loads the component view.
Update, try this:
LayoutInflater mInflater = (LayoutInflater)etContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resultsAutoComplete=(AutoCompleteTextView)mInflater.inflate(R.layout.your_view_id, this, true);
It seems the problem is that I have 2 layouts with the same identifiers from different projects (one project is linked to the other), so when I'm trying to inflate a layout from one of the projects, I'm getting the layout with the same identifier in the other project. Anyway, thanks for trying to help.

Categories

Resources