Android – RecyclerView dentro do Fragment – NullPointerException

Eu estou tentando usar um RecyclerView dentro de um Fragment (para usá-lo em um ViewPager ), mas eu tenho um NullPointerException quando tento executar:

  Process: com.yomansk8.birthdaysoundboard, PID: 4448 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.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.view.View.measure(View.java:17430) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456) 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.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:5779) 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:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

Primeiro, esse é o layout do meu fragment, ele contém apenas um RecyclerView :

   

Este é o meu fragment:

 package com.yomansk8.birthdaysoundboard.Fragments; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.yomansk8.birthdaysoundboard.Adapters.RecyclerViewAdapter; import com.yomansk8.birthdaysoundboard.GenerateDatas; import com.yomansk8.birthdaysoundboard.MainActivity; import com.yomansk8.birthdaysoundboard.Models.ViewModel; import com.yomansk8.birthdaysoundboard.R; import com.yomansk8.birthdaysoundboard.ScrollManager; import java.util.List; /** * Created by Yohann on 09/03/2015. */ public class DiversFragment extends Fragment implements RecyclerViewAdapter.OnItemClickListener { private MediaPlayer mPlayer = null; private Toolbar toolbar; private RecyclerView recyclerView; private GridLayoutManager mLayoutManager; private RecyclerViewAdapter adapter; private List items; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_divers, container, false); toolbar = ((MainActivity)getActivity()).getToolbar(); recyclerView = (RecyclerView) v.findViewById(R.id.recyclerDivers); recyclerView.setHasFixedSize(true); items = GenerateDatas.getAmbianceList(); adapter = new RecyclerViewAdapter(items); /*toolbar.post(new Runnable() { @Override public void run() { ScrollManager manager = new ScrollManager(); manager.attach(recyclerView); manager.addView(toolbar, ScrollManager.Direction.UP); manager.setInitialOffset(toolbar.getHeight()); } });*/ Log.v("TEST", "onCreateView()"); return v; } @Override public void onViewCreated(View view , Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Log.v("TEST", "onViewCreated()"); mLayoutManager = new GridLayoutManager(getActivity(), 3); recyclerView.setLayoutManager(mLayoutManager); adapter.setOnItemClickListener(this); recyclerView.setAdapter(adapter); } @Override public void onItemClick(View view, ViewModel viewModel) { // Lire le son ici Integer sound = getResources().getIdentifier(viewModel.getSound(), "raw", getActivity().getPackageName()); playSound(sound); } private void playSound(int resId) { if(mPlayer != null) { mPlayer.stop(); mPlayer.release(); } mPlayer = MediaPlayer.create(getActivity(), resId); mPlayer.start(); } @Override public void onPause() { super.onPause(); if(mPlayer != null) { mPlayer.stop(); mPlayer.release(); } } } 

Eu também tentei colocar a boot do meu RecyclerView dentro do onCreateView e onViewCreated , sem sucesso.

Qualquer ideia?

EDITAR:

Aqui está o código do meu Adapter :

 package com.yomansk8.birthdaysoundboard.Adapters; import android.os.Handler; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.squareup.picasso.Picasso; import com.yomansk8.birthdaysoundboard.R; import com.yomansk8.birthdaysoundboard.Models.ViewModel; import java.util.List; /** * Created by Yohann on 19/02/2015. */ public class RecyclerViewAdapter extends RecyclerView.Adapter implements View.OnClickListener { private List items; private OnItemClickListener onItemClickListener; public RecyclerViewAdapter(List items) { this.items = items; } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false); v.setOnClickListener(this); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { ViewModel item = items.get(position); holder.text.setText(item.getText()); holder.image.setImageBitmap(null); Integer mImage = holder.image.getResources().getIdentifier(item.getImage(), "drawable", holder.image.getContext().getPackageName()); Picasso.with(holder.image.getContext()).load(mImage).into(holder.image); holder.itemView.setTag(item); } @Override public int getItemCount() { return items.size(); } @Override public void onClick(final View v) { // Give some time to the ripple to finish the effect if (onItemClickListener != null) { new Handler().postDelayed(new Runnable() { @Override public void run() { onItemClickListener.onItemClick(v, (ViewModel) v.getTag()); } }, 200); } } protected static class ViewHolder extends RecyclerView.ViewHolder { public ImageView image; public TextView text; public ViewHolder(View itemView) { super(itemView); image = (ImageView) itemView.findViewById(R.id.image); text = (TextView) itemView.findViewById(R.id.text); } } public interface OnItemClickListener { void onItemClick(View view, ViewModel viewModel); } } 

Eu acho que você tinha usado uma revisão antiga de recyclerview assim como eu. Adotei a revisão recyclerview-20.0.0 da primeira versão de lançamento e, claro, a exceção ocorrendo. então eu atualizo meu sdk android, em seguida, copie a versão mais recente 22.1.1 (ambos os support-v4 e recyclerview-v7 jars) em bibliotecas do meu projeto, então o meu trabalho de projeto.

Para evitar que os caras da subsequência da minha equipe confundam as versões, eu nomeio que dois flasks incluem as informações de revisão:

flasks no diretório libs do meu projeto:

android-support-v4-22.1.1.jar

android-support-v7-recyclerview-22.1.1.jar




Antes disso, também descobri uma solução na revisão 20.0.0 , criei um widget chamado MyRecyclerView que se estendia do RecyclerView e, em seguida, MyRecyclerView um novo LayoutManager em construtores.

 public class MyRecyclerView extends RecyclerView { public MyRecyclerView(Context context) { this(context, null); } public MyRecyclerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setLayoutManager(new LayoutManager() { @Override public LayoutParams generateDefaultLayoutParams() { return null; } }); } } 

Pode funcionar, mas não foi a abordagem perfeita.



BTW, como disse @yigit em sua resposta , isso é uma falta de integração com o IDE e, no futuro, haverá uma melhor integração .

Experimente este layout para o seu fragment: