0

I have the following segment of code in a Fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_smartzone_category, container, false);

    final Context context = getActivity();

    mPresenter = new SmartzonePresenter(context, this);
    //
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner);
    mProgressBar.setVisibility(View.VISIBLE);

    // set list
    mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    mStaggeredLayoutManager.setSpanCount(3);

    mRecyclerView = (RecyclerView) root.findViewById(R.id.smartzonecategorylist);
    mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
    mRecyclerView.setVisibility(View.INVISIBLE);;

    // display list
    mListAdapter = new VideoCategoryAdapter(context, mCategories);
    mRecyclerView.setAdapter(mListAdapter);
    mListAdapter.setOnItemClickListener(new VideoCategoryAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            // get the category
            VideoCategory vc = mListAdapter.getItem(position);
            // display the sub-smartzone category page
            mCallback.onSelectCategory(vc, mCategories);
        }
    });

    //
    retrieveVideoList();

    return root;
}

Where mRecyclerView is declared at the header of the Fragment class.

The layout corresponds to this XML file:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<ProgressBar
    android:id="@id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyle"
    android:layout_centerInParent="true" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/smartzonecategorylist"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</FrameLayout>

However after cleaning/rebuilding and the likes I continuously receive this Logcat error:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                         at com.kidmixapp.commoncode.fragments.SmartzoneCategoryFragment.onCreateView(SmartzoneCategoryFragment.java:125)

Which refers to this line:

mRecyclerView.setLayoutManager(mStaggeredLayoutManager);

I also did some evaluation with debugging lines to see what root.findViewById(R.id.smartzonecategorylist); returned, and it is null?

I am not sure what is causing this. Other searches on similar issues have yielded no fixes I could find.

FIX:

It appears changing the XML Layout to refer to @id/android:list and change the findViewById to findViewById(android.R.id.list); like such fixed it. I do not know why.

NEW XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<ProgressBar
    android:id="@id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>

<android.support.v7.widget.RecyclerView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

NEW FRAGMENT ONCREATEVIEW

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_smartzone_category, container, false);

    final Context context = getActivity();

    mPresenter = new SmartzonePresenter(context, this);
    //
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner);
    mProgressBar.setVisibility(View.VISIBLE);

    // set list
    mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    mStaggeredLayoutManager.setSpanCount(3);

    mRecyclerView = (RecyclerView) root.findViewById(android.R.id.list);
    mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
    mRecyclerView.setVisibility(View.INVISIBLE);;

    // display list
    mListAdapter = new VideoCategoryAdapter(context, mCategories);
    mRecyclerView.setAdapter(mListAdapter);
    mListAdapter.setOnItemClickListener(new VideoCategoryAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            // get the category
            VideoCategory vc = mListAdapter.getItem(position);
            // display the sub-smartzone category page
            mCallback.onSelectCategory(vc, mCategories);
        }
    });

    //
    retrieveVideoList();

    return root;
}
Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64

1 Answers1

-1

In your layout XML,your code is:

<ProgressBar
android:id="@id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true" />

But I think the code Should Be:

<ProgressBar
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true" />
李茂杰
  • 7
  • 2