I'm trying to get a Staggered Grid layout using a RecyclerView
in one of my ViewPagers fragments. When I start the app, it immediately crashes and throws this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference at xxx.HomeActivity.onCreate(HomeActivity.java:39) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
My first thought is that this might be caused by the order in which the views are loaded but i'm not sure. I have a ViewPager
with 2 fragments. I have the RecyclerView
declared in the first fragments xml, like so:
res/layout/fragment_home.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/home_grid"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Heres the relative parts of my Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
setupToolbar();
setupTabs();
HomeGridAdapter adapter = new HomeGridAdapter(Contacts.createFakeContacts(20));
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.home_grid);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
}
Here is the first fragments Java file:
public class HomeFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static HomeFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
I dont think i need to post the RecyclerViews adapter code as I dont think that is part of the issue, but if its needed I can.
Any input on this issue is appreciated!
EDIT: As requested, heres my activitys xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="75dp"
style="@style/ToolbarStyle"
>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="scrollable"/>
</android.support.v7.widget.Toolbar>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>