1

When i am overriding this method in fragment then it is showing error like

The method dispatchTouchEvent(MotionEvent) is undefined for the type Fragment

This is my fragment i have created.....

public class ChatterContainerFragment extends Fragment implements
        SimpleGestureListener {
}

in this i am trying to override...

@Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        // Call onTouchEvent of SimpleGestureFilter class
        this.detector.onTouchEvent(me);
        return super.dispatchTouchEvent(me);
}

When I am overriding in FragmentActivity then its working fine. But for fragment it is not working.

Please help me.

Thanks!

0

1 Answer 1

1

There are two ways to do it,

1) You can add a TouchListener on the fragment View after creating the View. That's the most simple way.

2) You know that Fragments are attached to the Activity so you can override the dispatchTouchEvent method in the activity and pass actions from there. Like this,

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag("FRAGMENT_TAG");
    myFragment.doSomething();

    return super.dispatchTouchEvent(ev);
}

UPDATE

You need to do this to access a Dragment method from an Activity,

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>(); 
8
  • I tried the second one but I am getting NullPointerException.
    – Sanni Raj
    Jul 16, 2015 at 11:41
  • @Override public boolean dispatchTouchEvent(MotionEvent ev) { ChatterContainerFragment chatterContainerFragment = (ChatterContainerFragment) getSupportFragmentManager() .findFragmentByTag(StockalUtility.MAIN_FRAGMENT); chatterContainerFragment.dispatchEvent(ev); return super.dispatchTouchEvent(ev); }
    – Sanni Raj
    Jul 16, 2015 at 11:42
  • I have overriden this method in MainActivity and from dispatchTouchEvent(), I am calling fragment method as you told. but getting exception
    – Sanni Raj
    Jul 16, 2015 at 11:45
  • FATAL EXCEPTION: main Process: com.stockal, PID: 5795 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.stockal.ChatterContainerFragment.dispatchEvent(android.view.MotionEvent)' on a null object reference at com.stockal.MainActivity.dispatchTouchEvent(MainActivity.java:53) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275) at android.view.View.dispatchPointerEvent(View.java:8583)
    – Sanni Raj
    Jul 16, 2015 at 11:46
  • I have declared specific_function_name inside fragment class, and calling that method from MainActivity using fragment.<specific_function_name>();. But getting NullPointerException
    – Sanni Raj
    Jul 16, 2015 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.