Reputation: 505
I have this NullPointer exeption when I launch my RecyclerView fragment but I can't seem to find why it isn't stored inside my variable.
This is my complete code:
public class LocationFragment extends Fragment {
FragmentNameFactory nameFactory = new FragmentNameFactory();
List<LocationData> locationData = new ArrayList<>();
LocationRecyclerAdapter locationRecyclerAdapter = new LocationRecyclerAdapter(locationData);
public LocationFragment(){
//Fragment needs a constructor, can be empty
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
((MainActivity) getActivity()).setFragmentName(nameFactory.getNameNL(6));
View view = inflater.inflate(R.layout.flocation, container, false);
locationData.add(new LocationData("a", "a", "a", "b", 4.55555, 3.39023, "e"));
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.locationRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(locationRecyclerAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
locationRecyclerAdapter.notifyDataSetChanged();
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstance){
super.onViewCreated(view, savedInstance);
}
@Override
public void onActivityCreated(Bundle savedInstance){
super.onActivityCreated(savedInstance);
}
}
flocationRecycle.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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/locationRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</RelativeLayout>
LocationRecyclerAdapter:
public class LocationRecyclerAdapter extends RecyclerView.Adapter<LocationRecyclerAdapter.LocationViewHolder> {
List<LocationData> locationDataList;
public class LocationViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView textViewName;
public LocationViewHolder(View view) {
super(view);
cardView = (CardView) view.findViewById(R.id.card);
textViewName = (TextView) view.findViewById(R.id.text_view_names);
}
}
public LocationRecyclerAdapter(List<LocationData> locationsDataList){
this.locationDataList = locationsDataList;
}
@Override
public LocationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.flocation, parent, false);
LocationViewHolder locationViewHolder = new LocationViewHolder(view);
return locationViewHolder;
}
@Override
public void onBindViewHolder(LocationViewHolder holder, int position) {
holder.textViewName.setText(locationDataList.get(position).getName());
}
@Override
public int getItemCount() {
return locationDataList.size();
}
}
flocation.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="120dp">
<android.support.v7.widget.CardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="102dp">
<TextView
android:id="@+id/text_view_names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"></TextView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
The code crashes on this line: recyclerView.setHasFixedSize(true);
with a nullpointer like I said above but I can't find why recyclerView is empty
Upvotes: 2
Views: 112
Reputation: 869
I was reviewing your code and I found that you inflated the wrong xml file. See that flocationRecycle.xml is the correct one that has to be.
This is your onCreate implementation:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
((MainActivity) getActivity()).setFragmentName(nameFactory.getNameNL(6));
View view = inflater.inflate(R.layout.flocation, container, false);
locationData.add(new LocationData("a", "a", "a", "b", 4.55555, 3.39023, "e"));
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.locationRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(locationRecyclerAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
locationRecyclerAdapter.notifyDataSetChanged();
return view;
}
Please try do that and let me know if there is more issues.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
((MainActivity) getActivity()).setFragmentName(nameFactory.getNameNL(6));
View view = inflater.inflate(R.layout.flocationRecycle, container, false);
locationData.add(new LocationData("a", "a", "a", "b", 4.55555, 3.39023, "e"));
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.locationRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(locationRecyclerAdapter);
return view;
}
I'm wondering if your adapter depend of a network call. In that case you have to create a Listener and after you receive the data then call recyclerView.setAdapter().
Upvotes: 1
Reputation: 3940
There is many things need to be concern in your code:
RecyclerView.setLayoutManager()
should be call before RecyclerView.setAdapter()
RecyclerView.setAdapter()
, no need to call notifyDataSetChanged()
againThis could lead to NullPointerException
.
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.flocation, parent, false);
Because it's hard for your ViewGroup
to figure out what the context is, try to declare Context
field inside RecyclerViewAdapter
instead.
Upvotes: -1
Reputation: 5251
The View you are inflating does not contain the RecyclerView you are trying to reference.
According the code you have provided, your RecyclerView is declared within flocationRecycle.xml, but you are inflating flocation.xml instead:
View view = inflater.inflate(R.layout.flocation, container, false);
flocation.xml does not contain any R.id.locationRecyclerView child view, so when you try to reference that RecyclerView, you will get null pointer exception.
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.locationRecyclerView); //there is no recycler view declared inside view
Upvotes: 1
Reputation: 607
Your RecyclerView is null because it's not found in your layout XML under your search id (findViewById). My guess is that either you point to the wrong XML or you're searching by the wrong view id.
Upvotes: -1
Reputation: 16769
You inflate the wrong layout, do:
View view = inflater.inflate(R.layout.flocationRecycle, container, false);
Upvotes: 2