public class WorkFragment extends Fragment {
List<CardViewItem> items = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("FRAGMENT", "Work Fragment started");
TypedArray icons = getResources().obtainTypedArray(R.array.project_icons);
TypedArray names = getResources().obtainTypedArray(R.array.project_names);
TypedArray descs = getResources().obtainTypedArray(R.array.project_descs);
for (int i=0;i<icons.length();i++){
items.add(new CardViewItem(icons.getDrawable(i),
names.getString(i),
descs.getString(i)));
}
icons.recycle();
names.recycle();
descs.recycle();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("FRAGMENT", "Work Fragment onCreateView");
View rootView = inflater.inflate(R.layout.fragment_work, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new ItemAdapter(items));
return inflater.inflate(R.layout.fragment_work, container, false);
}
}
Gives me
E/RecyclerView: No adapter attached; skipping layout
I have tried out all the solutions I had found (setting an empty adapter, moving the code elswhere, using a seperate thread) but to no avail. This should work on a normal activity so I guess maybe I'm doing something wrong.
the problem is mostly this line
return inflater.inflate(R.layout.fragment_work, container, false);
you should have
return rootView
With the first return you are inflating a new totally different view hierarchy, starting from fragment_work.xml, from the one which has an the RecyclerView correctly set up.
Related
I want to display my data inside a fragment but it seems like some methods are not applicable in fragments only in Activity. The text with * are causing me errors. How i can execute it on a fragment?
String urlAddress = "http://capstonproject.xyz/project/tourist/retrieve/adventure";
RecyclerView recyclerView;
public Fadventure() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_fadventure, container, false);
return inflater.inflate(R.layout.fragment_fadventure, container, false);
recyclerView = view.findViewById(R.id.adventure_list);
recyclerView.setLayoutManager(new LinearLayoutManager(***this***));
recyclerView.setItemAnimator(new DefaultItemAnimator());
new DBFetch(***Fadventure.this***,recyclerView).execute();
//return recyclerView;
}
instead of this try with getActivity().getApplication().
hope it helps.
In my fragment's onCreateView method, I am inflating a layout that contains a RecyclerView. However, when I try to reference the view, I get a NullPointerException at the following line:
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
What am I doing wrong?
Here is my code:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstance){
final StaggeredGridLayoutManager mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.list);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setHasFixedSize(true); //Data size is fixed - improves performance
mAdapter = new TravelListAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
return inflater.inflate(R.layout.start_fragment,container,false);
}
You are doing getActivity.findViewById(), which wouldn't work. You just created the view, but it is not attached to the activity yet. For this to work, you have to find the view in the fragment's view, not the activity's view.
First, inflate the fragment's view, and then do inflatedView.findViewById(R.id.list).
Example:
public View onCreateView(...){
View inflatedView = inflater.inflate(R.layout.start_fragment, container, false);
mRecyclerView = (RecyclerView) inflatedView.findViewById(R.id.list);
return inflatedView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
final StaggeredGridLayoutManager mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) view.findViewById(R.id.list);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setHasFixedSize(true);
mAdapter = new TravelListAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
super.onViewCreated(view, savedInstanceState);
}
this is what I would do
I m have a problem with my RecyclerView. I m trying to populate it with a list of friends names, however when i launch my fragment friends are not loaded. On the other side friends are loaded if i m entering debug mode.
My code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mKinveyClient = KinveyUtils.getClient(getContext());
mFriends = getUserFriends();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friend_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
mRecyclerView = (RecyclerView) view;
mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
mRecyclerView.setAdapter(new FriendRecyclerViewAdapter(mFriends));
}
return view;
}
private ArrayList<ArrayMap> getUserFriends() {
return (ArrayList<ArrayMap>) mKinveyClient.user().get(KinveyConstants.FRIENDS);
}
I feel like the friends are loading too slow, but all the needed data should be already in app by this time. Any idea how to fix it?
I am getting an error saying "No adapter attached; skipping layout", however my images are loading into my LayoutManager just fine. However, when i try to scroll down and load additional data, the application crashes with a NullPointerException. Here is my related code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
service.getPodcasts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(podcasts -> mRecyclerView.setAdapter(new PodcastsAdapter(getActivity(), podcasts)));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_podcasts, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.podcasts_recycler_view);
mLayoutManager = new GridLayoutManager(getActivity(), 3);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//mRecyclerView.setAdapter(mAdapter);
return rootView;
}
Any ideas on how to fix this?
You didn't attach the adapter because you create it after you try to attach it:
In my case:
mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
This question already has answers here:
recyclerview No adapter attached; skipping layout
(38 answers)
Closed 3 years ago.
logcat error : No adapter attached; skipping layout
I changed the context argument with getActivity(), but the app is still not running.
public class FragmentActivity extends Fragment {
private RecyclerView mRecyclerView;
private CountryAdapter mAdapter;
private LinearLayoutManager layoutManager;
public FragmentActivity(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
You didn't attach the adapter because you create it after you try to attach it:
mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
In my case this problem occurs because there was a view on my layout which moved my list container and it was displayed too small