如何在recyclerview中使用片段?

时间:2019-05-15 22:16:45

标签: android android-fragments android-recyclerview

当我尝试使用代码时如何解决错误和应用程序崩溃: 尝试在空对象引用上调用虚拟方法'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' 我尝试在一个片段中显示一个recyclerview,但是当我使用下面的代码时出现错误,应用崩溃。 谁能帮我解决=

public class dashboard_device extends Fragment{
    RecyclerView mRecicleView1;
    RecyclerView.LayoutManager mLayoutManager1;
    RecyclerView.Adapter mAdapter1;
    ArrayList<String> lista_show;
    String preadd;
    public dashboard_device() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


       ArrayList<String> lista_show = new ArrayList<String> ();


        dashboard_DB db1 = new dashboard_DB (getContext ());


        //recupero datbase DB
        db1.open();


        Cursor c = db1.ottieniTuttidati();


        if (c.moveToFirst()) {
            do {


                lista_show.add (c.getString(2));


            } while (c.moveToNext());


        }
        db1.close();
        //fine recupero dati da db
        // Inflate the layout for this fragment
        mRecicleView1=container.findViewById (R.id.Reclycler_dashboard);
        mRecicleView1.setHasFixedSize (true);
        mLayoutManager1 = new GridLayoutManager (getContext (),3);
        mAdapter1 = new adapter_dash (lista_show,getContext ());
        mRecicleView1.setLayoutManager (mLayoutManager1);
        mRecicleView1.setAdapter (mAdapter1);
        runanimation1(mRecicleView1,0);
        Log.d("tag", String.valueOf (lista_show));
        return inflater.inflate (R.layout.dashboard_device_layout, container, false);


    }
    private void runanimation1(RecyclerView mRecicleView, int type) {
        Context context=mRecicleView.getContext ();
        LayoutAnimationController controller = null;
        if(type==0)
        controller = AnimationUtils.loadLayoutAnimation         (context,R.anim.layout_animation);
        mRecicleView.setLayoutAnimation (controller);
        mRecicleView.getAdapter ().notifyDataSetChanged ();
        mRecicleView.scheduleLayoutAnimation ();


    }


}

1 个答案:

答案 0 :(得分:0)

您正在尝试创建视图元素。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View fragmentView =  inflater.inflate (R.layout.dashboard_device_layout, container, false);

   ArrayList<String> lista_show = new ArrayList<String> ();


    dashboard_DB db1 = new dashboard_DB (getContext ());


    //recupero datbase DB
    db1.open();


    Cursor c = db1.ottieniTuttidati();


    if (c.moveToFirst()) {
        do {


            lista_show.add (c.getString(2));


        } while (c.moveToNext());


    }
    db1.close();
    //fine recupero dati da db
    // Inflate the layout for this fragment
    mRecicleView1=fragmentView.findViewById (R.id.Reclycler_dashboard);
    mRecicleView1.setHasFixedSize (true);
    mLayoutManager1 = new GridLayoutManager (getContext (),3);
    mAdapter1 = new adapter_dash (lista_show,getContext ());
    mRecicleView1.setLayoutManager (mLayoutManager1);
    mRecicleView1.setAdapter (mAdapter1);
    runanimation1(mRecicleView1,0);
    Log.d("tag", String.valueOf (lista_show));
    return fragmentView;

}

此外,您不应在OnCreateView中执行数据库操作,因为它会产生空白屏幕效果,因为查询数据库后您将返回视图。