Do not get the rootLayoutContainer in a Fragment (Android 3.0 Preview)

Posted by Hawk66 on Stack Overflow See other posts from Stack Overflow or by Hawk66
Published on 2011-02-14T12:41:27Z Indexed on 2011/02/19 15:25 UTC
Read the original article Hit count: 210

Filed under:
|
|

Hello,

I'm currently getting into the fragment API of the Android 3.0 Preview and have built the following minimal coding:

I have an Activty, which shall embed Fragment(s), which is currently implemented like this:

public class Cockpit extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cockpit);
}

public static class InfoFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        ViewGroup infoFragmentRoot = (ViewGroup) getActivity().findViewById(
                R.id.infoFragmentRoot) ;

        return inflater.inflate(R.id.infoFragment, container, false);
    }
}

}

The corresponding layout of the activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment android:name="test.android.ui.cockpit.Cockpit$InfoFragment"
        android:id="@+id/infoFragment"
        android:layout_weight="1"
        android:layout_width="10dp"
        android:layout_height="match_parent" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
                 android:layout_height="match_parent" android:padding="12dp" android:id="@+id/infoFragmentRoot" >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
        />
    </LinearLayout>
</fragment>

Now, I do not understand why the ViewGroup container in the onCreateView() in the internal class InfoFragment is a nullpointer, nor do I understand, why

ViewGroup infoFragmentRoot = (ViewGroup) getActivity().findViewById(
                R.id.infoFragmentRoot) ;

returns also null.

Thanks for feedback.

© Stack Overflow or respective owner

Related posts about android

Related posts about fragment