Reputation: 2004
I'm trying to mess with the new RecyclerView and whenever I try to run it, my app immediately crashes. It gives me NullPointerException for trying to access methods from android.support.v7.widget.RecyclerView
. I've looked at other posts and saw that most people didn't have compile 'com.android.support:recyclerview-v7:+'
but I tried that and it hasn't helped at all. Not really sure what to do at this point, any help would be appreciated. Here the error log: (I would post a picture but I don't have 10 rep yet)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Upvotes: 92
Views: 109347
Reputation: 37
I had this problem when using Butterknife , I was using fragment
For Fragment, it should be ButterKnife.bind(this,view);
For Activity ButterKnife.bind(this);
Upvotes: 0
Reputation: 1
I faced the same issue cause I had the RecyclerView in another layout and not on activity_main.xml.
By searching on some sites I got something and felt like sharing here
Add Id to Include tag in Activity_main.xml
<include id="includedRecyclerLayout"
....
</include>
call this layout before Searching view by ID in MainActivity.java
`ConstraintLayout includedLayout = findViewById(R.id.inckudedRecyclerLayout);
RecyclerViewrecyclerview=includedLayout.findViewById(R.id.RECYCLER_VIEW_ID)`;
Upvotes: 0
Reputation: 1
I had similar problem when I test my application on GenyMotion. After restart the device in GenyMotion problem was solved
Upvotes: 0
Reputation: 524
For those who are using: implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05'
make sure you are using this in your xml layout
I also experienced a crash when running my app below API 27. Here's a link to the message
Originally, I have created a style to change the font size of my textview inside the recyclerview that's why I created this code below: (I could have just called @dimen when changing the font size based on screen sizes)
and added this in my theme
After investigating, I found out that this is the one causing me an error and after removing the style, I no longer experiencing a crash.
I tried all the possible solutions I found here in stack overflow and almost gave up on this, thankfully in my curiosity I thought why not removing the style maybe this causes the crash and then boom the crash is gone.
Upvotes: 1
Reputation: 320
Recyclerview recylerview = findbyid(R.id.recyclerview) May be you have missed it
Upvotes: 0
Reputation: 485
In my case, I added only butterknife library and forget to add annotationProcessor. By adding below line to build.gradle (App module), solved my problem.
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
Upvotes: 0
Reputation: 215
using fragment
writing code in onCreateView()
RecyclerView recyVideo = view.findViewById(R.id.recyVideoFag);
using activity
writing code in onCreate() RecyclerView recyVideo = findViewById(R.id.recyVideoFag);
Upvotes: -1
Reputation: 663
I was using a Dependency Injection - ButterKnife and this problem arised because I had not binded the view.
So inside onCreate() I just inserted this line:
ButterKnife.bind(this);
My RecyclerView declaration with Dependency Injection:
@BindView(R.id.recyclerview)
RecyclerView recyclerView;
Upvotes: 1
Reputation: 2265
In Gradle implement this library :
implementation 'com.android.support:appcompat-v7:SDK_VER'
implementation 'com.android.support:design:SDK_VER'
implementation 'com.android.support:support-v4:SDK_VER'
implementation 'com.android.support:support-v13:SDK_VER'
implementation 'com.android.support:recyclerview-v7:SDK_VER'
In XML mention like this :
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView"/>
In Fragment we use recyclerView
like this :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.YOUR_LAYOUT_NAME, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
APICallMethod();
YOURADAPTER = new YOURADAPTER(getActivity(), YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
recyclerView.setAdapter(YOURADAPTER);
return rootView;
}
In Activity we use RecyclerView
like this :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT_NAME);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
APICallMethod();
YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
recyclerView.setAdapter(YOURADAPTER);
}
if you need horizontal RecyclerView
do this :
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
APICallMethod();
YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
recyclerView.setAdapter(YOURADAPTER);
if you want to use GridLayoutManager using RecyclerView
use like this :
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); // 3 is number of columns.
adapter = new MyRecyclerViewAdapter(this, data);
recyclerView.setAdapter(adapter);
Upvotes: 1
Reputation: 313
For me the problem was with my activity_main.xml(21) in which the recycleView didn't have an id.
activity_main.xml(21)
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3"
tools:listitem="@layout/transaction_list_row" />
activity_main.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/transac_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3"
/>
It worked when i added a android:id="@+id/transac_recycler_view" to the activity_main.xml(21) recycleView
Upvotes: 0
Reputation: 21
If I where you I will do this. onView(withId(android.R.id.list)).perform(RecyclerViewActions.scrollToPosition(3)); android.R.id.list change it to your list id and position will be your position inside your array.
Upvotes: -1
Reputation: 73
I had this problem when using Butterknife library. I had:
View rootView = inflater.inflate
(R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(rootView);
But the correct version is:
View rootView = inflater.inflate
(R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(this, rootView);
Upvotes: 1
Reputation: 1129
My problem was in my XML lyout I have an android:animateLayoutChanges set to true and I've called notifyDataSetChanged() on the RecyclerView's adapter in the Java code.
So, I've just removed android:animateLayoutChanges from my layout and that resloved my problem.
Upvotes: 1
Reputation: 10079
I got this issue due to wrong reference of RecyclerView
id.
recyclerView = (RecyclerView) findViewById(R.id.rv_followers_list);
to
recyclerView = (RecyclerView) findViewById(R.id.rv_search_list);
Upvotes: 3
Reputation: 1564
I think the problem in your Adapter
.
Make sure you have returned ViewHolder
in onCreateViewHolder()
.
Like below:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_leaderboard, parent, false);
ViewHolder view_holder = new ViewHolder(v);
return view_holder;
}
Upvotes: 0
Reputation: 37
recyclerView =
(RecyclerView) findViewById(R.id.recycler_view2);
Check with you recycler view ID, pointing to actual recycler view solved my issue
Upvotes: 2
Reputation: 3691
You need to use setLayoutManager
in the RecyclerView#onCreate()
method. Before adding recyclerView
to a view, it must have the LayoutManager
set.
private RecyclerView menuAsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
menuAsList = (RecyclerView) findViewById(R.id.recyclerView_mainMenu);
menuAsList.setLayoutManager(new LinearLayoutManager(Home.this));
}
Upvotes: 5
Reputation: 570
For me, I was having the same issue, the issue was that there was an unused RecyclerView in xml with view gone but I am not binding it to any adapter in Activity, hence the issue. It was solved as soon as I removed such unused recycler views in xml
i.e - I removed this view as this was not called in code or any adapter has been set
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_profileview_allactivities"
android:visibility="gone" />
Upvotes: 10
Reputation: 29434
This issue usually occurs when no LayoutManager
was provided for the RecyclerView
. You can do it like so:
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Upvotes: 210
Reputation: 3854
Since LinearLayoutManager is vertical by default, an easier way to do this is:
recyclerView.setLayoutManager(new LinearLayoutManager(context));
If you want to change the orientation you could use this constructor:
public LinearLayoutManager(Context context, int orientation, boolean reverseLayout);
Upvotes: 0
Reputation: 376
I experienced this crash even though I had the RecyclerView.LayoutManager
properly set. I had to move the RecyclerView
initialization code into the onViewCreated(...)
callback to fix this issue.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listing, container, false);
rootView.setTag(TAG);
return inflater.inflate(R.layout.fragment_listing, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ListingAdapter(mListing);
mRecyclerView.setAdapter(mAdapter);
}
Upvotes: 9
Reputation: 432
In my case it was not connected to 'final', but to the issue mentioned in @NemanjaKovačević comment to @aga answer. I was setting a layoutManager on data load and that was the cause of the same crash. After moving the layoutManager setup to onCreateView of my fragment the issue was fixed.
Something like this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
...
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler);
mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
Upvotes: 26