问题描述
我试图使用 RecyclerView
内部的片段
(把它用在一个 ViewPager
),但我得到了一个 NullPointerException异常
当我尝试执行:
流程:com.yomansk8.birthdaysoundboard,PID:4448
显示java.lang.NullPointerException:尝试调用虚拟方法无效android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView $州,在一个空对象引用INT,INT)
在android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
在android.view.View.measure(View.java:17430)
在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
在android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
在android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
在android.view.View.measure(View.java:17430)
在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2560)
在android.view.View.measure(View.java:17430)
在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779)
在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)
在android.view.Choreographer.doCallbacks(Choreographer.java:580)
在android.view.Choreographer.doFrame(Choreographer.java:550)
在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)
在android.os.Handler.handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5221)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
首先,这是我的片段的布局,它只是包含 RecyclerView
:
< XML版本=1.0编码=UTF-8&GT?;
< android.support.v7.widget.RecyclerView的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / recyclerDivers
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ATTR / actionBarSizepaddingTop =
机器人:clipToPadding =FALSE/>
这是我的片段:
包com.yomansk8.birthdaysoundboard.Fragments;
进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.support.annotation.Nullable;
进口android.support.v4.app.Fragment;
进口android.support.v7.app.ActionBar;
进口android.support.v7.widget.GridLayoutManager;
进口android.support.v7.widget.LinearLayoutManager;
进口android.support.v7.widget.RecyclerView;
进口android.support.v7.widget.Toolbar;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口com.yomansk8.birthdaysoundboard.Adapters.RecyclerViewAdapter;
进口com.yomansk8.birthdaysoundboard.GenerateDatas;
进口com.yomansk8.birthdaysoundboard.MainActivity;
进口com.yomansk8.birthdaysoundboard.Models.ViewModel;
进口com.yomansk8.birthdaysoundboard.R;
进口com.yomansk8.birthdaysoundboard.ScrollManager;
进口的java.util.List;
/ **
*在2015年9月3日的创Yohann。
* /
公共类DiversFragment扩展片段实现RecyclerViewAdapter.OnItemClickListener {
私人MediaPlayer的MPLAYER = NULL;
私人工具条工具栏;
私人RecyclerView recyclerView;
私人GridLayoutManager mLayoutManager;
私人RecyclerViewAdapter适配器;
私人列表<视图模型>项目;
@覆盖
公共查看onCreateView(LayoutInflater充气,@Nullable的ViewGroup容器,@Nullable捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.fragment_divers,集装箱,假);
工具栏=((MainActivity)getActivity())getToolbar()。
recyclerView =(RecyclerView)v.findViewById(R.id.recyclerDivers);
recyclerView.setHasFixedSize(真正的);
项= GenerateDatas.getAmbianceList();
适配器=新RecyclerViewAdapter(项目);
/*toolbar.post(new的Runnable(){
@覆盖公共无效的run(){
ScrollManager经理=新ScrollManager();
manager.attach(recyclerView);
manager.addView(工具栏,ScrollManager.Direction.UP);
manager.setInitialOffset(toolbar.getHeight());
}
}); * /
Log.v(测试,onCreateView());
返回伏;
}
@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
super.onViewCreated(查看,savedInstanceState);
Log.v(测试,onViewCreated());
mLayoutManager =新GridLayoutManager(getActivity(),3);
recyclerView.setLayoutManager(mLayoutManager);
adapter.setOnItemClickListener(本);
recyclerView.setAdapter(适配器);
}
@覆盖公共无效onItemClick(查看视图,视图模型视图模型){
//里拉勒的儿子ICI
。整数声音= getResources()则getIdentifier(viewModel.getSound(),生,getActivity()getPackageName());
playSound(音);
}
私人无效playSound(INT渣油){
如果(MPLAYER!= NULL){
mPlayer.stop();
mPlayer.release();
}
MPLAYER = MediaPlayer.create(getActivity(),渣油);
mPlayer.start();
}
@覆盖
公共无效的onPause(){
super.onPause();
如果(MPLAYER!= NULL){
mPlayer.stop();
mPlayer.release();
}
}
}
我也试图把我的 RecyclerView
初始化的 onCreateView
和 onViewCreated ,但没有成功。
你知道吗?
编辑:
下面是我的适配器
的code:
包com.yomansk8.birthdaysoundboard.Adapters;
进口android.os.Handler;
进口android.support.v7.widget.RecyclerView;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ImageView;
进口android.widget.TextView;
进口com.squareup.picasso.Picasso;
进口com.yomansk8.birthdaysoundboard.R;
进口com.yomansk8.birthdaysoundboard.Models.ViewModel;
进口的java.util.List;
/ **
*在19/02/2015创建者Yohann。
* /
公共类RecyclerViewAdapter扩展RecyclerView.Adapter< RecyclerViewAdapter.ViewHolder>实现View.OnClickListener {
私人列表<视图模型>项目;
私人OnItemClickListener onItemClickListener;
公共RecyclerViewAdapter(名单<视图模型>项目){
this.items =项目;
}
公共无效setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
@覆盖公共ViewHolder onCreateViewHolder(ViewGroup中父,诠释viewType){
视图V = LayoutInflater.from(parent.getContext())膨胀(R.layout.item_recycler,父母,假)。
v.setOnClickListener(本);
返回新ViewHolder(V);
}
@覆盖公共无效onBindViewHolder(ViewHolder持有人,INT位置){
视图模型项目= items.get(位置);
holder.text.setText(item.getText());
holder.image.setImageBitmap(空);
整数mImage = holder.image.getResources()则getIdentifier(item.getImage(),可拉伸,holder.image.getContext()getPackageName()。)。
。Picasso.with(holder.image.getContext())负载(mImage)。走进(holder.image);
holder.itemView.setTag(项目);
}
@覆盖公众诠释getItemCount(){
返回items.size();
}
@覆盖公共无效的onClick(最终查看V){
//给予一定时间的波动来完成的效果
如果(onItemClickListener!= NULL){
新的处理程序()。postDelayed(新的Runnable(){
@覆盖公共无效的run(){
onItemClickListener.onItemClick(ⅴ,(视图模型)v.getTag());
}
},200);
}
}
受保护的静态类ViewHolder扩展RecyclerView.ViewHolder {
公众ImageView的形象;
公共TextView的文字;
公共ViewHolder(查看ItemView控件){
超(ItemView控件);
图像=(ImageView的)itemView.findViewById(R.id.image);
文=(TextView的)itemView.findViewById(R.id.text);
}
}
公共接口OnItemClickListener {
无效onItemClick(查看视图,视图模型视图模型);
}
}
我看你使用的旧版本recyclerview的我一样。我通过了修订 recyclerview-20.0.0
第一个发行版本,当然还有例外发生。所以我升级我的Android SDK,然后复制这个最新版本 22.1.1
(均支持-v4和recyclerview-V7罐)到我的项目的库,然后我的项目工作。
在为prevent我的团队的序列家伙会比较混乱的版本,我命名的两个罐子,包括修订信息:
在我的项目的libs目录罐子:
在这之前,我也想不出办法的修订的 20.0.0 ,我创建了一个名为 MyRecyclerView
一个部件从RecyclerView延伸,然后新的一个粗略的布局管理的构造。
公共类MyRecyclerView扩展RecyclerView {
公共MyRecyclerView(上下文的背景下){
这(背景下,NULL);
}
公共MyRecyclerView(上下文的背景下,ATTRS的AttributeSet){
这(背景下,ATTRS,0);
}
公共MyRecyclerView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
setLayoutManager(新布局管理(){
@覆盖
公众的LayoutParams generateDefaultLayoutParams(){
返回null;
}
});
}
}
它可以工作,但不是完美的方法。
顺便说一句,作为@yigit在他的回答说,这是缺少整合与IDE 和在未来,将会有更好的整合
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);
}
}
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 :
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.
这篇关于Android的 - RecyclerView片段内 - NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!