如何获得片段活动?

时间:2019-07-30 09:57:41

标签: java android android-recyclerview android-fragmentactivity

我有两个问题。

  1. 我在一个类中使用“活动”,并在我的片段中将数据发送到该类。在片段中如何提交活动?

  2. 如何一起使用swipemenulistview和recyclerview?我要做一个滚动的recyclerview,但是我不能,应该使用哪个库或者应该遵循哪个路径?

HomeFragment.java

touchListener = new RecyclerTouchListener(
                    /*(HERE I TRIED) (IS THAT CORRECT?)*/ getActivity(),
                    allListRecyclerView
                );
touchListener
   .setClickable(new RecyclerTouchListener.OnRowClickListener() {
       @Override
       public void onRowClicked(int position) {
           Toast.makeText(getContext(), allListModels.get(position).getListname(), Toast.LENGTH_SHORT).show();
       }

       @Override
       public void onIndependentViewClicked(int independentViewID, int position) {

       }
   })
   .setSwipeOptionViews(R.id.delete_task,R.id.edit_task)
   .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
       @Override
       public void onSwipeOptionClicked(int viewID, int position) {
           switch (viewID){
               case R.id.delete_task:
                   allListModels.remove(position);
                   homeListAdapter.setTaskList(allListModels);
                   break;
               case R.id.edit_task:
                   Toast.makeText(getContext(),"Edit Not Available",Toast.LENGTH_SHORT).show();
                   break;

           }
       }
   });
}

RecyclerTouchListener.java

public RecyclerTouchListener(Activity a, RecyclerView recyclerView) {
    this.act = a;
    /*...*/
}

Logcat:

019-07-30 12:48:54.252 17839-17839/com.example.forev.huaweitodolist E/InputEventReceiver: Exception dispatching input event.
2019-07-30 12:48:54.252 17839-17839/com.example.forev.huaweitodolist E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-07-30 12:48:54.253 17839-17839/com.example.forev.huaweitodolist E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.support.v7.widget.RecyclerView$OnItemTouchListener.onInterceptTouchEvent(android.support.v7.widget.RecyclerView, android.view.MotionEvent)' on a null object reference
        at android.support.v7.widget.RecyclerView.dispatchOnItemTouchIntercept(RecyclerView.java:2932)
        at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2978)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2175)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
        at android.view.View.dispatchPointerEvent(View.java:10243)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
2019-07-30 12:48:54.253 17839-17839/com.example.forev.huaweitodolist D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-07-30 12:48:54.253 17839-17839/com.example.forev.huaweitodolist E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.forev.huaweitodolist, PID: 17839
    java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.support.v7.widget.RecyclerView$OnItemTouchListener.onInterceptTouchEvent(android.support.v7.widget.RecyclerView, android.view.MotionEvent)' on a null object reference
        at android.support.v7.widget.RecyclerView.dispatchOnItemTouchIntercept(RecyclerView.java:2932)
        at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2978)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2175)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
        at android.view.View.dispatchPointerEvent(View.java:10243)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)

1 个答案:

答案 0 :(得分:0)

在这样的活动中创建静态方法

public static void getActivity()
 {
      // call this wer you want and get data
 }