fragment 中 mRecyclerView.setLayoutManager()报错???????

Fragment中 onCreateView代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_fragment2,
            container, false);
    initDatas();
    mRecyclerView = view.findViewById(R.id.item2);
    mRVAdapter = new RVAdapter(getActivity(),mData);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));//错误行;
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setAdapter(mRVAdapter);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
    return view;
}

adapter代码:

public class RVAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private Context context;
    private List<String> data;

    public RVAdapter(Context context , List<String> data){
        this.context = context;
        this.data = data;

    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { //创建ViewHolder
        View view = LayoutInflater.from (viewGroup.getContext ()).inflate (R.layout.item2,viewGroup,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int pos) {   // 绑定viewholder
        holder.tv.setText(data.get(pos));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView tv;

    public MyViewHolder(@NonNull View arg0) {
        super(arg0);
        tv = arg0.findViewById(R.id.item2);
    }
}

所报错误:

2019-04-18 21:18:25.483 25820-25820/cn.daun.player E/AndroidRuntime: FATAL EXCEPTION: main

    Process: cn.daun.player, PID: 25820

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

        at cn.daun.player.view.Fragment2.onCreateView(Fragment2.java:37)

        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)

        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)

        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)

        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)

        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)

        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)

        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)

        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)

        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)

        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)

        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)

        at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)

        at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)

        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)

        at android.view.View.measure(View.java:23169)

        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)

        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)

        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问