Join FlipAndroid.COM Telegram Group: https://t.me/joinchat/F_aqThGkhwcLzmI49vKAiw


Reciclar vista – java.lang.NullPointerException

Estoy recibiendo la excepción siguiente mientras que intento exhibir la visión de reciclar:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1216) at android.view.View.onDrawScrollBars(View.java:13900) at android.view.View.draw(View.java:16275) at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2440) at android.view.View.updateDisplayListIfDirty(View.java:15192) at android.view.View.getDisplayList(View.java:15215) at android.view.View.draw(View.java:15990) at android.view.ViewGroup.drawChild(ViewGroup.java:3708) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3502) at android.view.View.draw(View.java:16272) at android.view.View.updateDisplayListIfDirty(View.java:15192) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:362) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3692) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3671) at android.view.View.updateDisplayListIfDirty(View.java:15152) at android.view.View.getDisplayList(View.java:15215) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:307) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:313) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:352) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2811) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2634) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2222) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6726) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777) at android.view.Choreographer.doCallbacks(Choreographer.java:590) at android.view.Choreograph 

Y mi xml es este:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/global_screen_linear_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/lilAdvancedSearchParent" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/gray_general" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <LinearLayout android:id="@+id/lilAdvancedSearch" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="6" android:gravity="center_vertical" > <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_white" /> <TextView android:id="@+id/txvAvencedSearch" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="4.5" android:text="@string/global_fragment_advenced_search" android:textColor="@color/white" android:textSize="@dimen/global_screen_layout_text_view_advenced_search" android:textStyle="bold" /> </LinearLayout> <EditText android:id="@+id/edtSearch" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="9" android:drawableLeft="@drawable/navigation_search" android:ems="10" android:imeOptions="actionSearch" android:inputType="text" android:textColor="@color/white" android:textColorHint="@color/white" android:textSize="15sp" > <requestFocus /> </EditText> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="800dp" android:scrollbars="vertical" > </android.support.v7.widget.RecyclerView> </LinearLayout> 

Y mi código java es este:

 public class TendersFragment extends BaseFragment{ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.global_screen_layout, container, false); initUI(v); initListeners(); setEditSearchHint(getString(R.string.tenders_fragment_search_tenders)); setAdvancedSearchBackground(getResources().getColor(R.color.green_general)); return v; } @Override protected void initUI(View view) { lsvTendersJobsMessages = (ListView) view.findViewById(R.id.lsvGlobal); mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view); setAdapterForListview(lsvTendersJobsMessages); edtSearch = (EditText) view.findViewById(R.id.edtSearch); txvAdvencedSearch = (TextView) view.findViewById(R.id.txvAvencedSearch); lilAdvancedSearch = (LinearLayout) view.findViewById(R.id.lilAdvancedSearch); lilAdvancedSearchParent = (LinearLayout) view.findViewById(R.id.lilAdvancedSearchParent); } @Override protected void setAdapterForListview(ListView lsv) { mRecyclerView.setHasFixedSize(true); mLinearLayoutManager = new LinearLayoutManager(getActivity()); mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLinearLayoutManager); ArrayList<ItemType> arlItems = new ArrayList<ItemType>(); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); arlItems.add(ItemType.MAIN_ITEM); // specify an adapter (see also next example) mainItemAdapter = new MainItemAdapter(arlItems, getActivity()); mRecyclerView.setAdapter(mainItemAdapter); } @Override protected void onServiceConnected() { // TODO Auto-generated method stub } protected void initListeners() { final AdvancedSearchFragment advancedSearchFragment = new AdvancedSearchFragment(); Bundle bundle = new Bundle(); bundle.putInt(FragmentConst.BUNDLE_KEY_INT_WHICH_FRAGMENT,FragmentConst.TENDERS_ID); advancedSearchFragment.setArguments(bundle); lilAdvancedSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getActivity().getSupportFragmentManager().beginTransaction() .add(android.R.id.content,advancedSearchFragment).addToBackStack(null) .commit(); } }); } } 

También estoy tratando de establecer el layout_height de la vista de reciclar como match_parent y wrap_contant pero aún así obtener el mismo error

5 Solutions collect form web for “Reciclar vista – java.lang.NullPointerException”

Aquí es cómo finalmente resuelto este problema.

Quité android:scrollbars="vertical" de la declaración de RecyclerView:

 <android.support.v7.widget.RecyclerView android:id="@+id/task_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/> 

Quite esta línea del xml:

Android: scrollbars = "vertical"

Creo que debe inicializado RecyclerView en el código java enlace este

 //1. get a reference to recyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); //2. set layoutManger recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 3. create an adapter MyAdapter mAdapter = new MyAdapter(itemsData); // 4. set adapter recyclerView.setAdapter(mAdapter); 

¿Declara recyclerView en cualquier archivo de diseño que no haya tratado con controlador de java (Actividad / Fragmento)?

Declaro el diseño de dos recicladores en dos fragmentos, proceso el primer reciclador en mis códigos java, no he tratado con el segundo. Después de eliminar la declaración recyclerView en fragment2-layout-file, resolvió el problema del puntero nulo.

Reemplazar

 mLinearLayoutManager = new LinearLayoutManager(getActivity()); mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLinearLayoutManager); 

con

 final LinearLayoutManager mLayoutManager= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true); mRecyclerView.setLayoutManager(mLayoutManager); 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.