So I have an Activity which has a FragmentPagerAdapter and inside each fragment I have two other Fragment (here called a and b). Fragment a is a ListFragment, while fragment b is just a normal fragment that displays the value.
There are two issues here…
1.) I cannot grab views from the Activity in Fragment B
2.) If I comment out the grabs, instead of showing Fragment A on a phone followed by B once an item is selected, IT shows both on the same viewpager.
However, I can’t seem to load my layouts properly. However if I remove b everything seems to work fine…
The Activity loads the following layout…
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainpager"/>
</LinearLayout>
My First Fragment looks as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment android:name="com.gleason.apa.fragment.BarListFragment"
android:id="@+id/bar_list_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.gleason.apa.fragment.BarEditFragment"
android:id="@+id/bar_edit_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Fragment A
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar Name: " />
<TextView
android:id="@+id/bar_name_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: " />
<TextView
android:id="@+id/bar_address_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Fragement B
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar Name" />
<EditText
android:id="@+id/bar_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Name"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address" />
<EditText
android:id="@+id/bar_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Address"/>
</LinearLayout>
<Button
android:id="@+id/submitBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Game"/>
</LinearLayout>
However, when I try to compile and run I get the following…
android.view.InflateException: Binary XML file line #6: Error inflating class fragment
java.lang.NullPointerException
Any ideas?
UPDATE:
So the null pointer exception comes from BarEditFragment…
@Override
public void onActivityCreated(Bundle savedInstanceState) {
....
name = (EditText) activity.findViewById(R.id.bar_name);
address = (EditText) activity.findViewById(R.id.bar_address);
Button submit = (Button) activity.findViewById(R.id.submitBar);
}
All of those come back null.
Also if I remove those, the fragments show both, even on my phone. So it shows two small fragments side by side, instead of the 2 “screens” like it is supposed to.
Also this did not work…
activity.getFragmentManager().findFragmentById(R.id.bar_edit_fragment).getView().findViewById(R.id.bar_name);
The thing I was missing is you need to create layouts that tell if it is single or dual pane. The best way to figure this out is to use the NewsReader example as a template.