Reputation: 12664
I'm trying to implement a simple fragment in android.
I've tried various approaches and the only time I don't get an error is when I remove the fragment from the layout altogether. Other than that, I've removed inflate and tried to create a layout manually and that didn't work either.
Only removing the fragment
from the layout seems to make the application load, no matter what else I do, the application crashes.
I keep getting the following error:
12-29 04:52:07.493: E/AndroidRuntime(11717): java.lang.RuntimeException: Unable to resume activity {com.p5sys.android.jump/com.p5sys.android.jump.lib.activities.DisplayContactList}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
I also get the following error after the above errors stacktrace (just sharing incase it helps).
12-29 04:52:07.493: E/AndroidRuntime(11717): Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f090023, tag null, or parent id 0x0 with another fragment for com.p5sys.android.jump.lib.fragment.HeaderFragment
My layout contact_layout.xml
is a LinearLayout with a fragment
and ListView
in it:
<fragment
android:id="@+id/headerFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
class="com.p5sys.android.jump.lib.fragment.HeaderFragment" />
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@color/transparent"
android:cacheColorHint="#00000000" >
</ListView>
The fragment layout header_fragment.xml
is:
<TableRow>
<ImageButton
android:id="@+id/btnSettings"
android:gravity="left"
android:hint="@string/label_settings"
android:onClick="btnAction_launchSettings"
android:padding="3dip"
android:src="@drawable/ic_menu_add" />
<TextView
android:id="@+id/headerText"
android:gravity="center"
android:height="50dip"
android:padding="3dip"
android:textSize="20sp"
android:textStyle="bold"
android:text="Blank" />
<ImageButton
android:id="@+id/btnAdd"
android:background="@drawable/disconnectbutton"
android:gravity="right"
android:hint="@string/label_add"
android:onClick="btnAction_launchAddNew"
android:padding="3dip"
android:src="@drawable/ic_menu_add" />
</TableRow>
My Fragment class:
package com.p5sys.android.jump.lib.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.p5sys.android.jump.lib.R;
/***
* Fragment
* @author Ali
*
*/
public class HeaderFragment extends Fragment {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate layout
return inflater.inflate(R.layout.header_fragment, container, false);
// LinearLayout ll = new LinearLayout(getActivity());
// TextView tv = new TextView(getActivity());
// tv.setText("hello world");
// ll.addView(tv);
// return ll;
}
}
In my Activity I'm not doing anything special, I've tried using regular Activity
as well as FragmentActivity
and I've gotten the same problem.
Any help would be much appreciated.
Upvotes: 0
Views: 12491
Reputation: 934
I never had much luck with this approach myself. Plus, you're going to have to do it differently anyway if you want the possibility to instantiate many types of fragments in the same place within the layout, or swipe through many instances of Fragments with your finger (via ViewPager
), etc.
So here's what I do: 1) Use a FrameLayout
to reserve space for the Fragment.
<FrameLayout
android:id="@+id/where_i_want_my_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
2) Instantiate the Fragment with the following Java code:
FragmentManager fragMgr = getFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
if (null == fragMgr.findFragmentByTag(myFragTag)) {
xact.add(R.id.where_i_want_my_fragment, MyDerivedFragment.newInstance(), myFragTag).commit();
}
myFragTag
is a String value you need to come up with. It's the tag name you want to assign to the Fragment instance, similar to the "id" attribute in HTML. If you only have one Fragment instance, then you can call it anything you want; otherwise it's best to designate the tag name as something related to what that specific instance is appearing in.
Don't forget the commit()
part at the very end, otherwise it won't work!
Upvotes: 2
Reputation: 7387
If you want to interact at all with your fragment (and maybe even if you don't) your activity needs to be a fragment activity.
The reason you're getting this error is that TableRow is not a valid parent. You need to use some kind of ViewGroup layout. I have no idea why you're using tablerow here, but you shouldn't be using it. Try a horizontal linear layout instead. If the goal is to have even spacing, give everything a layout weight of 1 in xml.
Otherwise what you've posted looks ok...
Upvotes: 0