I am getting this error when trying to set my layoutmanager for my recyclerview. I have created a layout manager and set my recyclerview to use it. For some reason the layoumanager is null.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jcaseydev.popularmovies/com.jcaseydev.popularmovies.ui.ReviewsActivity}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2625)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5969)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
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.jcaseydev.popularmovies.ui.ReviewsFragment.onCreateView(ReviewsFragment.java:65)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6686)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2588)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5969)
at java.lang.reflect.Method.invoke(Native Method)
Here is the recylerview class.
public ReviewsFragment(){}
RecyclerView mRecyclerView;
RecyclerView.LayoutManager linearLayoutManager;
RecyclerView.Adapter mAdapter;
private int movieId;
private List<Reviews> movieReviews = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_review, container, false);
FetchMovieReviews fmr = new FetchMovieReviews();
// fmr.execute();
//get intent
Intent intent = getActivity().getIntent();
//key for the intent extra
String MOVIE_ID = "movie_id";
if(intent != null && intent.hasExtra(MOVIE_ID)){
movieId = intent.getIntExtra(MOVIE_ID, 49026);
}
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview);
linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
movieReviews.add(0, new Reviews("test", "TESTING"));
movieReviews.add(1, new Reviews("test", "TESTING"));
movieReviews.add(2, new Reviews("tset", "TESTING"));
mAdapter = new MyAdapter(movieReviews);
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private List<Reviews> mDataset;
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView;
}
}
public MyAdapter(List<Reviews> reviews){
mDataset = reviews;
}
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.review_list_item, parent, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
#Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mDataset.get(position).getAuthor());
}
#Override
public int getItemCount() {
return mDataset.size();
}
}
I'm not sure what the problem could be.
Here is the review_fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/reviews_recyclerview"/>
</LinearLayout>
Here is the activity_review xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/review_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
The LayoutManager isn't null, your RecyclerView is.
You're inflating R.layout.activity_review instead of R.layout.review_fragment.
Because of this, mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview); results in mRecyclerView being null.
Thus, make sure you inflate R.layout.review_fragment:
View rootView = inflater.inflate(R.layout.review_fragment, container, false);
In this line you're actually inflating the view of the activity:
View rootView = inflater.inflate(R.layout.activity_review, container, false);
But the recycler view you seek to find is not in that layout, but in the fragment layout. Change that line to:
View rootView = inflater.inflate(R.layout.review_fragment, container, false);
Related
I'm making a tabbed activity with different fragments and one of them contains a recyclerview with just some names at the moment. I have no idea what I've done wrong, I get a warning every time on starting the app that says no adapter attached, skipping layout. I've created and set the adapter to the recyclerview in my fragment, created an adapter with viewholder, and created a class to set the name on the recyclerview.
Here is my fragment class:
public class fragment_main extends Fragment {
View view;
private RecyclerView mainrecyclerview;
private List<MainSchedule> listStr;
public fragment_main() {}
#NonNull
#Override
public View onCreateView(LayoutInflater inflater, #NonNull ViewGroup container, #NonNull Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
mainrecyclerview = (RecyclerView) view.findViewById(R.id.todayRecyclerView);
MainRecyclerViewAdapter mainAdapter = new MainRecyclerViewAdapter(getActivity(), listStr);
mainrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mainrecyclerview.setAdapter(mainAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listStr = new ArrayList<>();
listStr.add(new MainSchedule("one"));
listStr.add(new MainSchedule("two"));
listStr.add(new MainSchedule("three"));
}
}
fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment_main">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/todayRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="250dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerviewadapter class:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
Context mContext;
List<MainSchedule> mData;
public MainRecyclerViewAdapter(Context context, List<MainSchedule> data){
this.mContext = context;
this.mData = data;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.mainschedule_cards, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mData.get(position).getName());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textViewClass);
}
}
}
I've tried many ways of creating the adapter and binding the layout manager to the recyclerview, but nothing seems to work and I get the same error each time.
Problem seems with onCreateView because you're supposed to return view
Replace your fragment_main with code below
public class fragment_main extends Fragment {
private RecyclerView mainrecyclerview;
private List<MainSchedule> listStr;
public fragment_main() {
}
#NonNull
#Override
public View onCreateView(LayoutInflater inflater, #NonNull ViewGroup container, #NonNull Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listStr = new ArrayList<>();
listStr.add(new MainSchedule("one"));
listStr.add(new MainSchedule("two"));
listStr.add(new MainSchedule("three"));
mainrecyclerview = (RecyclerView) view.findViewById(R.id.todayRecyclerView);
MainRecyclerViewAdapter mainAdapter = new MainRecyclerViewAdapter(getActivity(), listStr);
mainrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mainrecyclerview.setAdapter(mainAdapter);
}
}
I am trying to populate recyclerView with Java objects using Firestore. The RecyclerView is within a fragment. I am using a recyclerViewAdapter which extends the FirestoreRecyclerAdapter class. When I run the code, the app crashes immediately.
Here's the error.
.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
Here's some of the fragment Java code:
public class EventsFragment extends Fragment{
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference eventRef = db.collection("Events");
private EventRecyclerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.events_fragment, container, false);
setUpRecyclerView();
return rootView;
}
private void setUpRecyclerView(){
Query query = eventRef.limit(8);
FirestoreRecyclerOptions<Event> options = new FirestoreRecyclerOptions.Builder<Event>()
.setQuery(query, Event.class)
.build();
adapter = new EventRecyclerAdapter(options);
RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
}
}
Here's the fragment xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Upcoming Events"
app:menu="#menu/main_menu"
android:background="#android:color/white"
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:clipToPadding="false"
android:divider="#null"
android:id="#+id/events_recycler_view"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here's the RecyclerAdapter code:
public class EventRecyclerAdapter extends FirestoreRecyclerAdapter<Event, EventRecyclerAdapter.EventHolder> {
public EventRecyclerAdapter(#NonNull FirestoreRecyclerOptions<Event> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull EventHolder holder, int position, #NonNull Event model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
}
#NonNull
#Override
public EventHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.events_list_item,
parent, false);
return new EventHolder(v);
}
class EventHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
public EventHolder(#NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.title);
textViewDescription = itemView.findViewById(R.id.description);
}
}
}
I believe the problem lies somewhere with the RecyclerView. I've been trying to find the problem for hours now. Is there something I'm overlooking? I just need it to run.
You already noticed that recyclerView is null when you try to set the LayoutManager in setUpRecyclerView()
This happens because you are trying to "find" the RecyclerView in the Activity's View tree before the Fragment's View has actually been attached to it:
RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.events_recycler_view);
The first Fragment lifecycle method where you can put this statement is onResume() (at least in my example app on an emulator running Android 9)
But you can configure the RecyclerView much earlier because you can "find" it in the Fragment's own View for example in onViewCreated():
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext());
// etc.
}
Please note that the view parameter in onViewCreated() represents the View which you can get everywhere in the Fragment by calling getView(). But only after onCreateView() has finished, because the purpose of this method is creating and "publishing" the Fragment's View in the first place.
Last not least, inside of onCreateView() you can "find" the RecyclerView by means of the newly inflated View:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.events_fragment, container, false);
//setUpRecyclerView();
RecyclerView recyclerView = (RecyclerView) rootView .findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(rootView.getContext());
// etc.
return rootView;
}
So maybe you'd best pass the root View as parameter into setUpRecyclerView() and use it as follows:
private void setUpRecyclerView(ViewGroup root){
Query query = eventRef.limit(8);
FirestoreRecyclerOptions<Event> options = new FirestoreRecyclerOptions.Builder<Event>()
.setQuery(query, Event.class)
.build();
adapter = new EventRecyclerAdapter(options);
RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext()));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
}
In this fragment,
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
rv.setNestedScrollingEnabled(false);
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm");
SunTimes suntime = SunTimes.compute().at(latlang.Lat,latlang.Lang).today().execute();
String sun_rise = localDateFormat.format(suntime.getRise());
String sun_set = localDateFormat.format(suntime.getSet());
Date sunnoon = suntime.getNoon();
System.out.println("SUNRISE "+ sun_rise);
TextView cityField = rootView.findViewById(R.id.tv_city);
TextView sunrise = rootView.findViewById(R.id.tv_sunrt);
TextView sunset = rootView.findViewById(R.id.tv_sunst);
cityField.setText("Hello World"); //Line 45
sunrise.setText(sun_rise);
sunset.setText(sun_set);
return rootView;
}
I am getting error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.phocast.BlankFragment.onCreateView(BlankFragment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
The question is while I have a normal string as a text, how it can get null point exception?
What am I doing wrong?
Update
the fragment_blank does not have the id, as its just an inflater:
<?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.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
That id is in item_blank.xml as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="120sp" >
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="120sp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_image"
android:layout_width="120sp"
android:layout_height="fill_parent"
android:scaleType="fitStart"
card_view:srcCompat="#drawable/property_image_3">
</ImageView>
<TextView
android:id="#+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_marginBottom="1sp"
android:layout_toRightOf="#+id/iv_image"
android:gravity="top"
android:paddingLeft="5sp"
android:text="Hello World"
android:textAppearance="#style/TextAppearance.AppCompat.Large">
</TextView>
.....
and which is defined in the Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_city);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.mTextView.setText(mDataset[position]);
holder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String currentValue = mDataset[position];
Log.d("CardView", "CardView Clicked: " + currentValue);
}
});
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
So I am expecting the cardview to be read from the adapter, as it is working for this case:
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
//
// #Override
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
//
// }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
rv.setNestedScrollingEnabled(false);
Weather_OWM.placeIdTask asyncTask = new Weather_OWM.placeIdTask(new Weather_OWM.AsyncResponse() {
public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise, String sun_set) {
TextView cityField = rootView.findViewById(R.id.tv_city);
TextView sunrise = rootView.findViewById(R.id.tv_sunrt);
TextView sunset = rootView.findViewById(R.id.tv_sunst);
cityField.setText(weather_city);
sunrise.setText(sun_rise);
sunset.setText(sun_set);
}
});
asyncTask.execute(Double.toString(latlang.Lat), Double.toString(latlang.Lang)); // asyncTask.execute("Latitude", "Longitude")
rv.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]{"Today", "Golden Hour", "Blue Hour", "Civil Twilight", "Nautical Twilight", "Astronomical Twilight", "Hello", "World"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
}
It's not about the string argument but the cityField TextView you're calling setText() on. You're initialising it with
rootView.findViewById(R.id.tv_city);
which returns null if the view is not found. So your fragment_blank layout does not have a view with id tv_city.
Hi guys I have the following error I cannot seem to resolve.
I have
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
The code I have is slightly different than from regular examples since I am doing this in a Fragment rather than in a Activity.
This is my code.
I have an xml for the whole Fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="mtr.MainActivityFragmentTwo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/grades"
android:textStyle="bold"
android:textSize="40dp"
android:gravity="center_horizontal"
android:layout_gravity="center"
/>
</FrameLayout>
Then I have a content_fragment which holds the RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/realm_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Then my code in my Fragments looks as follows
public class MainActivityFragmentTwo extends Fragment {
private Realm realm;
private RecyclerView recyclerView;
public MainActivityFragmentTwo() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main_activity_fragment_two, container, false);
}
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
realm.init(getActivity());
realm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) getActivity().findViewById(R.id.realm_recycler_view);
setupRecyclerView();
}
public void setupRecyclerView() {
Log.d("Found grades", "not showing grades tho");
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerView.setAdapter(new gradeRealmAdapter(this, realm.where(Grade.class).findAllAsync()));
recyclerView.setHasFixedSize(true);
}
}
the Adapter I created is
public class gradeRealmAdapter extends RealmRecyclerViewAdapter<Grade, gradeRealmAdapter.GradeViewHolder> {
private final MainActivityFragmentTwo activity;
public gradeRealmAdapter (MainActivityFragmentTwo activity, RealmResults<Grade> realmResults) {
super(activity.getActivity(), realmResults, true);
this.activity = activity;
}
#Override
public GradeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new GradeViewHolder(inflater.inflate(R.layout.gradeitem, parent, false));
}
#Override
public void onBindViewHolder(GradeViewHolder holder, int position) {
Grade grade = getData().get(position);
holder.title.setText(grade.toString());
}
class GradeViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
public TextView title;
public Grade grade;
public GradeViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.grade_text_view);
view.setOnLongClickListener(this);
}
public boolean onLongClick(View v) {
activity.deleteItem(grade);
return true;
}
}
}
The error comes from the Fragment class on the following line
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
I can't seem to resolve this.
Can anyone see what I am doing wrong?
I have an content_fragment which holds the recyclerview
Then you need to findViewById on that layout, not the Activity's.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_activity_fragment_two, container, false);
// Don't use getActivity().findViewById
recyclerView = (RecyclerView) rootView.findViewById(R.id.realm_recycler_view);
setupRecyclerView();
return rootView;
}
Then, you also have the realm initialization backwards.
realm.init(getActivity()); // This throws an exception as well
realm = Realm.getDefaultInstance(); // It is initialized afterwards
So, fix that too within onAttach, where you are guaranteed to have some Context
#Override
public void onAttach(Context context) {
super.onAttach(context);
realm = Realm.getDefaultInstance();
realm.init(context);
}
let's try to change
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
with
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
And make some update for onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.fragment_main_activity_fragment_two,container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.realm_recycler_view);
setupRecyclerView();
return rootView;
}
Inflate your recycler view on onViewCreated()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.fragment_main_activity_fragment_two,container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.realm_recycler_view);
setupRecyclerView();
return rootView;
}
Not in onCreate
I want to use custom ListView in extends from ListFragment class. After create new subClass extends from BaseAdapter into current class and set layout to that I get ... has stopped error.
list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
ResivedSMS.java :
public class ResivedSMS extends ListFragment {
private String testArray1[];
private String testArray2[];
private ListView listFragment;
public ResivedSMS() {
testArray1 = new String[] {
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555",
"6666666666",
};
testArray1 = new String[] {
"AAAAA",
"BBBBB",
"CCCCC",
"DDDDD",
"FFFFF",
"GGGGG",
};
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
listFragment = (ListView) inflater.inflate(
R.layout.list_fragment, container, false);
ViewResivedSMSDetailes customListView = new ViewResivedSMSDetailes(getActivity(),testArray1,testArray2);
listFragment.
setAdapter( customListView );
return listFragment;
}
#Override
public void onListItemClick(ListView list, View v, int position, long id) {
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
class ViewResivedSMSDetailes extends BaseAdapter
{
private LayoutInflater inflater;
private String[] values1;
private String[] values2;
private class ViewHolder {
TextView txt1;
TextView txt2;
}
public ViewResivedSMSDetailes(Context context,String[] values1,String[] values2)
{
this.values1=values1;
this.values2=values2;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return values1.length;
}
#Override
public Object getItem(int index) {
return values1[index];
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView ==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_fragment, null);
holder.txt1 = (TextView)convertView.findViewById(R.id.txt1);
holder.txt2 = (TextView)convertView.findViewById(R.id.txt2);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.txt1.setText(values1[position]);
holder.txt2.setText(values2[position]);
return convertView;
}
}
}
logcat Result:
08-24 10:33:34.675 349-349/ir.tsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout
at ir.tsms.ResivedSMS.onCreateView(ResivedSMS.java:50)
ResivedSMS.java:50 is:
listFragment = (ListView) inflater.inflate(
R.layout.list_fragment, container, false);
Whats my code problem and how to resolved Thanks?
problem:
inflater.inflate(R.layout.list_fragment, container, false);
You are inflating a LinearLayout not a ListView thus giving you ClassCastException
solution:
Since you are using ListFragment you can directly set the adapter, since ListFragment has its own default ListView. Set the adapter in the onCreate Method.
sample:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewResivedSMSDetailes customListView = new ViewResivedSMSDetailes(getActivity(),testArray1,testArray2);
setListAdapter( customListView ); //call the method if listFragment
}