问题
I'm trying to use a RecyclerView
inside of a Fragment
(to use it in a ViewPager
) but I got a NullPointerException
when I try to execute:
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)
First, this is the layout of my fragment, it's only containing a RecyclerView
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerDivers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
android:clipToPadding="false"/>
This is my 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<ViewModel> 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();
}
}
}
I've also tried to put the initialisation of my RecyclerView
inside of onCreateView
and onViewCreated
, with no success.
Any idea?
EDIT :
Here is the code of my 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<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {
private List<ViewModel> items;
private OnItemClickListener onItemClickListener;
public RecyclerViewAdapter(List<ViewModel> 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);
}
}
回答1:
I think you had used an old revision of recyclerview just like me. I adopted revision recyclerview-20.0.0
the first release version, and of course the exception occurring. so I upgrade my android sdk, then copy the lastest version 22.1.1
(both support-v4 and recyclerview-v7 jars) into my project's libs, then my project work.
In order to prevent the subsequence guys of my team get confusing of the versions, I naming that two jars to including the revision information :
jars in my project's libs directory :
android-support-v4-22.1.1.jar
android-support-v7-recyclerview-22.1.1.jar
Before that, I also figure out a solution in revision 20.0.0, I create a widget named MyRecyclerView
which extended from RecyclerView, then new a rough LayoutManager in constructors.
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;
}
});
}
}
It can work but wasn't the perfect approach.
BTW, as @yigit said in his answer, this is a lack of integration with the IDE and In the future, there will be better integration.
回答2:
Try this layout for your fragment:
<?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="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerDivers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
来源:https://stackoverflow.com/questions/28971623/android-recyclerview-inside-of-fragment-nullpointerexception