SwipeRecyclerview使用中一些常见错误处理方法【持续更新中】

引言

第一次接触SwipeRecyclerview,感觉这款中国人自己的框架对开发有很大的帮助,就在自己的第一个项目中运用它来实现一些功能,下面就是我所遇到的一些错误,希望能对各位有所帮助

 Process: com.example.admin.powercontrol, PID: 7985
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.powercontrol/com.example.admin.powercontrol.activity.ViewActivity}: java.lang.IllegalStateException: Cannot set menu creator, setAdapter has already been called.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.IllegalStateException: Cannot set menu creator, setAdapter has already been called.
        at com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView.checkAdapterExist(SwipeMenuRecyclerView.java:201)
        at com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView.setSwipeMenuCreator(SwipeMenuRecyclerView.java:262)
        at com.example.admin.powercontrol.activity.ViewActivity.init(ViewActivity.java:57)
        at com.example.admin.powercontrol.BaseActivity.onCreate(BaseActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
出现这个错误的原因是因为
swipeMenuRecyclerView.setSwipeMenuCreator(swipeMenuCreator)放在了
        swipeMenuRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);
        swipeMenuRecyclerView.setSwipeItemClickListener(swipeItemClickListener);
        swipeMenuRecyclerView.setAdapter(facilityAdapter);

后面

改正方法:

    
      swipeMenuRecyclerView.addItemDecoration(itemDecoration);
        // 设置侧滑菜单监听器。
        swipeMenuRecyclerView.setSwipeMenuCreator(swipeMenuCreator);//将监听器的设置放在前面即可
        swipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        swipeMenuRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);
        swipeMenuRecyclerView.setSwipeItemClickListener(swipeItemClickListener);
        swipeMenuRecyclerView.setAdapter(facilityAdapter);

你可能感兴趣的:(Android,常见错误,android)