Cannot make a static reference to the non-static method setOnTouchListener(View.OnTouchListener) from the type View

StackOverflow https://stackoverflow.com/questions/22356205

Pergunta

Scenario:

I've created an app drawer and placed a seekbar within it. This app drawer is made from app_drawer.xml whilst my fragment below is made up from main_activity.xml. The issue I'm having is, although the seekbar shows up perfectly and the app drawer pulls in and out as it should, when I drag it nothing happens (By which I mean the textview is NOT updated). Even though it should.

I've been told the below is a fix, but I keep getting the error:

Cannot make a static reference to the non-static method setOnTouchListener(View.OnTouchListener) from the type View

How do I get rid of the error?

Fix using proposed code:

     public class MainActivity extends Fragment {

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

                View rootView = inflater.inflate(R.layout.main_activity, container, false);
                View rootView2 = inflater.inflate(R.layout.app_drawer, container, false);



         final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);  
         SeekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());

         TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);

              sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   

              public void onProgressChanged(SeekBar bar, int progress,
                    boolean fromUser) {

 textProgress.setText("Progress: "+ String.valueOf(progress));
    }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }
              });

                return rootView;
            }
        }

    /**
     * This is just a workarround for make seekbar work on drawer
     */
    static class WorkarroundFixMovementOnDrawer implements View.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int action = event.getAction();
            DrawerLayout parentDrawer = getParentDrawer(v);

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow Drawer to intercept touch events.
                    if (parentDrawer != null) {
                        parentDrawer.requestDisallowInterceptTouchEvent(true);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    // Allow Drawer to intercept touch events.
                    if (parentDrawer != null) {
                        parentDrawer.requestDisallowInterceptTouchEvent(false);
                    }
                    break;
            }

            // Handle seekbar touch events.
            v.onTouchEvent(event);
            return true;

        }

        /**
         * Try to get DrawerLayout from parent
         * @param view view to search
         * @return the drawerLayout parent
         */
        public static DrawerLayout getParentDrawer(View view) {

            if (view != null) {

                ViewParent recursiveView = view.getParent();
                while (!(recursiveView instanceof DrawerLayout)) {

                    if (recursiveView == null) {
                        return null;
                    }

                    recursiveView = recursiveView.getParent();
                }

                return (DrawerLayout) recursiveView;

            }

            return null;

        }

    }

Orignal code:

       public class MainActivity extends Fragment {

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

                View rootView = inflater.inflate(R.layout.main_activity, container, false);
                View rootView2 = inflater.inflate(R.layout.app_drawer, container, false);



         final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);  
         TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);

              sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   

              public void onProgressChanged(SeekBar bar, int progress,
                    boolean fromUser) {

 textProgress.setText("Progress: "+ String.valueOf(progress));
    }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }
              });

                return rootView;
            }
        }

Proposed Fix:

seekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());


/**
 * This is just a workarround for make seekbar work on drawer
 */
static class WorkarroundFixMovementOnDrawer implements View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction();
        DrawerLayout parentDrawer = getParentDrawer(v);

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow Drawer to intercept touch events.
                if (parentDrawer != null) {
                    parentDrawer.requestDisallowInterceptTouchEvent(true);
                }
                break;

            case MotionEvent.ACTION_UP:
                // Allow Drawer to intercept touch events.
                if (parentDrawer != null) {
                    parentDrawer.requestDisallowInterceptTouchEvent(false);
                }
                break;
        }

        // Handle seekbar touch events.
        v.onTouchEvent(event);
        return true;

    }

    /**
     * Try to get DrawerLayout from parent
     * @param view view to search
     * @return the drawerLayout parent
     */
    public static DrawerLayout getParentDrawer(View view) {

        if (view != null) {

            ViewParent recursiveView = view.getParent();
            while (!(recursiveView instanceof DrawerLayout)) {

                if (recursiveView == null) {
                    return null;
                }

                recursiveView = recursiveView.getParent();
            }

            return (DrawerLayout) recursiveView;

        }

        return null;

    }

}
Foi útil?

Solução

setOntouchListener is a interface.

You have

final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);  
SeekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());

Should be

sk.setOnTouchListener(new WorkarroundFixMovementOnDrawer());

Coz You have

class WorkarroundFixMovementOnDrawer implements View.OnTouchListener 

Your WorkarroundFixMovementOnDrawer implements the interface View.OnTouchListener.

Outras dicas

Change

TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);

to

textProgress = (TextView)rootView.findViewById(R.id.TextView01);

and add an Object to your class

public class MainActivity extends Fragment {
TextView textProgress;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top