问题描述:

I need help for my first Android application. I have a single activity with an Action Bar at the top and Navigation Tabs. Because of these navigation tabs I had to convert my Activities into Fragments.

So one of those Activities (which is now a Fragment) held two fragments, one ListFragment on the Left and a DetailFragment on the right, so that the user is able to select an item from the list and directly sees details on the right.

But now as the Activity has been transformed into the Fragment, it's the Fragment that holds the List- and the DetailFragment. This works fine until I want to change the DetailsFragment.

First of all, here is the code of the Fragment holding the other two:

public class BlackboardFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

return inflater.inflate(R.layout.fragment_layout, container, false);

}

}

Here is the fragment_layout.xml file (saved in layout-land):

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

<fragment

android:id="@+id/interfaces"

android:layout_width="0px"

android:layout_height="match_parent"

android:layout_marginRight="20dp"

android:layout_weight="0.60"

class="org.fawkesrobotics.app.InterfaceFragment" />

<FrameLayout

android:id="@+id/viewer"

android:layout_width="0px"

android:layout_height="match_parent"

android:layout_weight="1" />

</LinearLayout>

So, the ListFragment is named InterfaceFragment, here is the code:

public class InterfaceFragment extends ListFragment {

boolean mDualPane;

// some other code

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setHasOptionsMenu(true);

create_list();

View viewerFrame = getActivity().findViewById(R.id.viewer);

mDualPane = viewerFrame != null

&& viewerFrame.getVisibility() == View.VISIBLE;

if (savedInstanceState != null) {

mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);

}

}

// some other code

}

This is, where it doesn't work. The Fragment actually shows the list (that is created in create_list() where the ListAdapter and everything else is set), but my problem is, that viewerFrame is null. I declared the id viewer in the xml file above, so I don't understand why he cannot find the id. Does it "belong" to the BlackBoardFragment or to my overall activity? Because in BlackboardFragment I also tried it with getActivity().findViewById and it worked. Why is InterfaceFragment not able to find it?

I also tested, if he really uses the fragment_layout.xml in layout-land and he does. So he should know the id viewer, but obviously doesn't.

If you need more code or the code from the DetailsFragment, I can post it.

Thank you in advance for your help! :)

网友答案:

use FragmentManager to get Fragment......

ExampleFragment fragment = (ExampleFragment)  getActivity().getFragmentManager().findFragmentById(R.id.example_fragment);
网友答案:

Nested Fragments are not supported by the Android SDK. That is, you can't have a "fragment within a fragment" in your Android application. Sorry.

EDIT:

Nested Fragments are now part of the official Android 4.2 SDK! Yay!

相关阅读:
Top