A
A
Andrey Kostin2015-01-03 10:18:39
Android
Andrey Kostin, 2015-01-03 10:18:39

Container == null in onCreateView method when creating static fragment?

There is an activity with one fragment, which is added via xml.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <fragment
        android:name="localhost.myweatherapp.ForecastFragment"
        android:id="@+id/forecast_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Fragment layout.
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list_view_forecast"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</FrameLayout>

And the onCreateView method
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Create some dummy data for the ListView.  Here's a sample weekly forecast
        String[] data = {
                "Mon 6/23 - Sunny - 31/17",
                "Tue 6/24 - Foggy - 21/8",
                "Wed 6/25 - Cloudy - 22/17",
                "Thurs 6/26 - Rainy - 18/11",
                "Fri 6/27 - Foggy - 21/10",
                "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
                "Sun 6/29 - Sunny - 20/7"
        };
        List<String> weekForecastData = new ArrayList<String>(Arrays.asList(data));
        ArrayAdapter forecastAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecastData);
        ListView forecastListView = (ListView)container.findViewById(R.id.list_view_forecast);
        forecastListView.setAdapter(forecastAdapter);

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

So, for reasons that are not clear to me, the container argument is null.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
�
Олег Гамега, 2015-01-03
@doska666

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
        .....
         ListView forecastListView = (ListView)rootView .findViewById(R.id.list_view_forecast);
       ........
        return rootView;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question