La aplicación se bloquea al intentar usar RecyclerView en Android 5.0

Estoy tratando de meterme con el nuevo RecyclerView y cada vez que bash ejecutarlo, mi aplicación se bloquea inmediatamente. Me da NullPointerException para tratar de acceder a los métodos de android.support.v7.widget.RecyclerView . Miré otras publicaciones y vi que la mayoría de la gente no compile 'com.android.support:recyclerview-v7:+' pero lo intenté y no me ayudó en absoluto. No estoy muy seguro de qué hacer en este punto, cualquier ayuda sería apreciada. Aquí el registro de errores: (publicaría una imagen pero aún no tengo 10 representantes)

 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) 

Este problema suele ocurrir cuando no se proporciona LayoutManager para RecyclerView . Puedes hacerlo así:

 final LinearLayoutManager layoutManager = new LinearLayoutManager(context); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); 

En mi caso, no estaba conectado a ‘final’, pero al problema mencionado en @ NemanjaKovačević comentar a @aga answer. Estaba configurando un layoutManager en la carga de datos y esa fue la causa del mismo locking. Después de mover la configuración de layoutManager a onCreateView de mi fragmento, el problema se solucionó.

Algo como esto:

 @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); 

Para mí, estaba teniendo el mismo problema, el problema era que había un RecyclerView sin usar en xml con la vista perdida, pero no lo estoy vinculando a ningún adaptador en la Actividad, de ahí el problema. Fue resuelto tan pronto como eliminé las vistas de reciclado no usadas en xml

es decir, eliminé esta vista ya que no se llamó en el código o se configuró cualquier adaptador

  

Experimenté este locking a pesar de tener el RecyclerView.LayoutManager correctamente configurado. Tuve que mover el código de inicialización de RecyclerView a la callback onViewCreated(...) para solucionar este problema.

 @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); } 

Necesita usar setLayoutManager en el método RecyclerView#onCreate() . Antes de agregar recyclerView a una vista, debe tener el conjunto LayoutManager .

  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)); } 

Como LinearLayoutManager es vertical de forma predeterminada, una forma más fácil de hacerlo es:

recyclerView.setLayoutManager(new LinearLayoutManager(context));

Si desea cambiar la orientación, puede usar este constructor:

 public LinearLayoutManager(Context context, int orientation, boolean reverseLayout); 
 recyclerView = (RecyclerView) findViewById(R.id.recycler_view2); 

Verifique con usted la ID de la vista de reciclador, señalando la vista de reciclador actual resuelto mi problema

 package com.example.om.recycler_view; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListmovieList= new ArrayList<>(); private RecyclerView recyclerView; private MoviesAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); recyclerView=(RecyclerView)findViewById(R.id.recycler_view); mAdapter=new MoviesAdapter(movieList); RecyclerView.LayoutManager mLayoutManager= new LinearLayoutManager(this); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter); prepareMovieData(); } private void prepareMovieData() { Movie movie=new Movie("Mad Max: Fury Road", "Action & Adventure", "2015"); movieList.add(movie); movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015"); movieList.add(movie); movie = new Movie("Shaun the Sheep", "Animation", "2015"); movieList.add(movie); movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015"); movieList.add(movie); movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015"); movieList.add(movie); movie = new Movie("Up", "Animation", "2009"); movieList.add(movie); movie = new Movie("Star Trek", "Science Fiction", "2009"); movieList.add(movie); movie = new Movie("The LEGO Movie", "Animation", "2014"); movieList.add(movie); movie = new Movie("Iron Man", "Action & Adventure", "2008"); movieList.add(movie); movie = new Movie("Aliens", "Science Fiction", "1986"); movieList.add(movie); movie = new Movie("Chicken Run", "Animation", "2000"); movieList.add(movie); movie = new Movie("Back to the Future", "Science Fiction", "1985"); movieList.add(movie); movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981"); movieList.add(movie); movie = new Movie("Goldfinger", "Action & Adventure", "1965"); movieList.add(movie); movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014"); movieList.add(movie); mAdapter.notifyDataSetChanged(); } } ERROR is: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.om.recycler_view/com.example.om.recycler_view.MainActivity}: 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:2348) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5345) 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:947) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742) 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.example.om.recycler_view.MainActivity.onCreate(MainActivity.java:28) at android.app.Activity.performCreate(Activity.java:6012) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5345) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) How can solve??????? 

Creo que el problema en tu Adapter . Asegúrese de haber devuelto ViewHolder en onCreateViewHolder() . Como abajo:

 @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; } 

Recibí este problema debido a una referencia incorrecta de la identificación de RecyclerView .

 recyclerView = (RecyclerView) findViewById(R.id.rv_followers_list); 

a

 recyclerView = (RecyclerView) findViewById(R.id.rv_search_list); 

Mi problema estaba en mi XML Lyout. Tengo un android: animateLayoutChanges establecido en true y he llamado a notifyDataSetChanged () en el adaptador de RecyclerView en el código de Java.

Entonces, acabo de eliminar Android: animateLayoutChanges de mi diseño y eso resuelve mi problema.

Tuve este problema al usar la biblioteca Butterknife. Tuve:

 View rootView = inflater.inflate (R.layout.fragment_recipe_detail_view, container, false); ButterKnife.bind(rootView); 

Pero la versión correcta es:

 View rootView = inflater.inflate (R.layout.fragment_recipe_detail_view, container, false); ButterKnife.bind(this, rootView); 

Si yo fuera tuyo, haré esto. onView (withId (android.R.id.list)). perform (RecyclerViewActions.scrollToPosition (3)); android.R.id.list cámbialo a tu id de lista y la posición será tu posición dentro de tu matriz.