【问题标题】:Cannot resolve symbol 'setOnClickListener' - AndroidStudio无法解析符号'setOnClickListener' - Android Studio
【发布时间】:2015-08-22 10:37:19
【问题描述】:

相对而言,我是 Java/Android 编程的新手/初学者。我一直在尝试这样做,所以当我在应用程序中按下给定按钮时,它会产生 DTMF 音,但是当我尝试使用 setOnTouchListener 时,Android Studio 会向我显示该错误。它还给了我一个 MotionEvent 错误,指出 Expression expected

以下是代码的重要部分:

boolean pressedCCW = false;
    class SendCCWTone extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... arg0){
            ToneGenerator toneGen;
            toneGen = new ToneGenerator(AudioManager.STREAM_DTMF,100);
            while(pressedCCW){
                toneGen.startTone(ToneGenerator.TONE_DTMF_1);
            }
            toneGen.stopTone();
            toneGen.release();
            createLog("CCW");
            return null;
        }
    }

final Button buttonCCW = (Button) findViewById(R.id.counter_clockwise);
    buttonCCW.setOnTouchListener(new View.OnTouchListener(){// Where the error is
        @Override
        public boolean onTouch(View v, MotionEvent event){// Where the other error is located
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(pressedCCW == false){
                        pressedCCW = true;
                        new SendCCWTone().execute();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    pressedCCW = false;
            }
            return true;
        }
    });

【问题讨论】:

    标签: java android android-studio android-asynctask android-button


    【解决方案1】:

    您正在setOnClickListener 内部创建OnTouchListener。如果您需要TouchListener,那么您应该使用setOnTouchListener 而不是setOnClickListener 注册

    buttonCCW.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(pressedCCW == false){
                            pressedCCW = true;
                            new SendCCWTone().execute();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        pressedCCW = false;
                }
                return true;
            }
        });
    

    【讨论】:

      【解决方案2】:

      如果将 setOnTouchListener 放在 Activity 的 onCreate() 方法中,则可以解决此问题。

      【讨论】:

        【解决方案3】:

        您可以在 XML 中设置 onClick 并指向一个方法,而不是使用setOnClickListener(它做同样的事情并且看起来更好)。在这种情况下,你会有一个像produceSound这样的方法:

        public void produceSound(View view) {
          // your onClick method
        }
        

        在 Activity 的 XML 中,找到该按钮 counter_clockwise 的位置,然后在按钮的 XML 中添加:android:onClick="produceSound"

        如果您好奇,请点击此处:How exactly does the android:onClick XML attribute differ from setOnClickListener?

        但是,如果您使用的是 onTouch,那么您将不得不坚持其他人的建议。 XML 不支持 android:onTouch 属性。

        【讨论】:

        • 是的,在这种情况下,我正在使用 onTouch,因为我希望在按住按钮时让应用程序产生声音。谢谢!
        • 如果您解决了问题,请发布您的答案——仅供其他可能需要此信息的人参考。 (和往常一样没问题。)
        【解决方案4】:

        尝试将此添加到您的代码中:

         implements View.OnTouchListener
        

        并使用 setOnTouchListner 而不是 setOnClickListener。

        buttonCCW.setOnListener(new View.OnTouchListener(){// Where the error is
            @Override
            public boolean onTouch(View v, MotionEvent event){// Where the other error is located
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(pressedCCW == false){
                            pressedCCW = true;
                            new SendCCWTone().execute();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        pressedCCW = false;
                }
                return true;
            }
        });
        

        【讨论】:

        • Sushrita,我应该把“implements View.OnTouchListener”放在哪里?谢谢! :)
        • 在你的班级开始像这样:'public class MainActivity extends ActionBarActivity implements View.OnTouchListener {'
        猜你喜欢
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        • 1970-01-01
        • 2015-12-17
        • 2017-04-20
        相关资源
        最近更新 更多