Android - Fragment中的RecyclerView - NullPointerException

时间:2015-03-10 18:40:55

标签: java android android-fragments android-recyclerview

我试图在RecyclerView内使用Fragment(在ViewPager中使用它)但是当我尝试使用NullPointerException时我得到 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) 执行:

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"/>

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

}

这是我的片段:

RecyclerView

我还尝试将onCreateView初始化为onViewCreatedAdapter,但没有成功。

有什么想法吗?

编辑:

以下是我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}}

1 个答案:

答案 0 :(得分:0)

为您的片段尝试此布局:

<?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>