Don't get rootLayoutContainer in snippet (Android 3.0 Preview)

Currently, I ended up in the Android 3.0 Preview fragment API and created the following minimum encoding:

I have an Activity that should insert fragments (s) that are currently implemented as follows:

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);
    }
}

}

Corresponding action layout:

<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 don’t understand why the ViewGroup container in onCreateView () in the InfoFragment inner class is nullpointer, and I don’t understand why

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

also returns null.

Thanks for the feedback.

+3
source share
2 answers

. , <fragment>. . onCreateView() , XML . XML . onCreateView() - :

    View v = inflater.inflate(R.layout.frag1, container, false);
    TextView text1 = (TextView) v.findViewById(R.id.text1);
    text1.setText( myTextData );
    return v;

, attach inflate() - false? Android .

, onActivityCreated(). , infoFragmentRoot null onCreateView(). , , <fragment>.

, , onInflate() . , , onCreateView() ( setArguments() getArguments()). , , (, ), onInflate() onCreateView(), . . http://code.google.com/p/android/issues/detail?id=14796.

XML (, frag1.xml), , onCreateView(). , onInflate().

+8

onCreate , . - , .

0

Source: https://habr.com/ru/post/1791996/


All Articles