AlexS
AlexS

Reputation: 974

Fragment is given a null on recyclerView in onCreateView()

Application based on one activity and some fragments. When fragment starts application is crashed with log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.alexsuvorov.paistewiki/ru.alexsuvorov.paistewiki.StartDrawer}: java.lang.NullPointerException
                  ...
               Caused by: java.lang.NullPointerException
                  at ru.alexsuvorov.paistewiki.Fragment.NewsFragment.onCreateView(NewsFragment.java:42)

There i just initialize some array positions and just try display it

Now i edit code

public class NewsFragment extends Fragment {

private static final String TAG = "NewsFragment";
private ImageSliderAdapter sliderAdapter;
private List<NewsMonth> monthArray;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.news_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view,
                          Bundle savedInstanceState){
    super.onViewCreated(view, savedInstanceState);
    ViewPager viewPager = view.findViewById(R.id.view_pager);
    sliderAdapter = new ImageSliderAdapter(getContext());
    viewPager.setAdapter(sliderAdapter);

    initializeData();
    RecyclerView recyclerView = view.findViewById(R.id.newsList);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(llm);
    NewsAdapter adapter = new NewsAdapter(monthArray); /*this.getContext(), R.layout.news_item, monthArray*/
    recyclerView.setAdapter(adapter);
}

private void initializeData() {
    monthArray = new ArrayList<>();
    monthArray.add(new NewsMonth("www.ya.ru", "Январь", null));
    monthArray.add(new NewsMonth("www.on.ru", "Февраль", null));
    monthArray.add(new NewsMonth("www.ti.ru", "Март", null));
}

}

My XML files - recycle list and card item: ... .... ...



<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/newsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
android:padding="8dp">
<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    layout_constraintEnd_toStartOf="@+id/newsList"
    android:layout_width="match_parent"
    android:layout_height="103dp"
    app:layout_constraintEnd_toStartOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</android.support.v4.view.ViewPager>
<ScrollView
    android:id="@+id/newsScroll"
    android:layout_width="match_parent"
    android:layout_height="433dp"
    android:paddingEnd="@dimen/fab_margin"
    app:layout_anchor="@+id/view_pager"
    app:layout_anchorGravity="bottom"
    app:layout_constraintTop_toBottomOf="@id/view_pager"
    tools:layout_constraintTop_toBottomOf="@+id/view_pager"
    android:paddingRight="@dimen/fab_margin">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/newsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top|bottom|left|right"
        android:orientation="vertical"
        app:layout_anchor="@+id/view_pager"
        app:layout_anchorGravity="bottom|center"
        app:layout_constraintTop_toBottomOf="@id/view_pager"
        tools:layout_editor_absoluteX="3dp"
        tools:layout_editor_absoluteY="112dp">
    </android.support.v7.widget.RecyclerView>
</ScrollView>



and card item: a few characters of text so that the stack overflow does not swear a few characters of text so that the stack overflow does not swear a few characters of text so that the stack overflow does not swear a few characters of text so that the stack overflow does not swear


<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/black"
app:cardCornerRadius="8dp"
app:cardUseCompatPadding="true"
app:contentPadding="0dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/news_label"
        android:layout_width="255dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@color/colorAccent"
        tools:text="Who is.. James bond??!" />


    <TextView
        android:id="@+id/news_category"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/news_label"
        android:layout_alignBottom="@+id/news_label"
        android:layout_toEndOf="@+id/news_label"
        android:layout_toRightOf="@+id/news_label"
        android:background="@android:color/holo_green_light"
        tools:text="Artist" />

</RelativeLayout>

<TextView
    android:id="@+id/month_name"
    android:layout_width="150dp"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimaryDark"
    android:fontFamily="sans-serif-condensed"
    android:gravity="center"

    android:textSize="16sp"
    android:textStyle="bold"
    tools:text="Июнь" />

Upvotes: 2

Views: 696

Answers (1)

John Bonda
John Bonda

Reputation: 119

Do not write findViewById() statements inside OnCreateView. Only use OnCreateView() for inflating the view. Write the findViewById() methods in the OnViewCreated() method, which calls as soon as the view creation is completed.
By writing findViewById() in onCreateView(), you are trying to find a view while the container View is still in the process of creation and your view might not have been created yet which results in returning a null.

Upvotes: 1

Related Questions