简体   繁体   English

无法解析符号'setOnClickListener' - AndroidStudio

[英]Cannot resolve symbol 'setOnClickListener' - AndroidStudio

I'm relatively a rookie/beginner with Java/Android programming. 我是Java / Android编程的新手/初学者。 I've been trying to make it so while I press a given button in my application it produces a DTMF tone, but when I try to use setOnTouchListener the Android Studio shows me that error. 我一直试图这样做,当我在我的应用程序中按下一个给定的按钮时它会产生一个DTMF​​音,但是当我尝试使用setOnTouchListener时,Android Studio会向我显示该错误。 It also gives me an error for MotionEvent which states Expression expected 它还为MotionEvent提供了一个错误, 表示期望表达式

Here are the important parts of the code: 以下是代码的重要部分:

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;
        }
    });

You are creating OnTouchListener inside of setOnClickListener . 您正在OnTouchListener中创建setOnClickListener If you need TouchListener then you should register using setOnTouchListener instead of setOnClickListener 如果需要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;
        }
    });

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

Try to add this to your code : 尝试将此添加到您的代码中:

 implements View.OnTouchListener

and use setOnTouchListner instead of setOnClickListener. 并使用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;
    }
});

Rather than use setOnClickListener , you can set onClick in the XML and point to a method (it does the same thing and looks nicer). 您可以在XML中设置onClick并指向一个方法(它做同样的事情并且看起来更好),而不是使用setOnClickListener In this case, you'd have a method like produceSound: 在这种情况下,你有一个像produceSound这样的方法:

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

and in the activity's XML, find where that button, counter_clockwise , is and add: android:onClick="produceSound" to the button's XML. 并在活动的XML中,找到该按钮的位置, counter_clockwise ,并将: android:onClick="produceSound"到按钮的XML中。

More here if you're curious: How exactly does the android:onClick XML attribute differ from setOnClickListener? 更多这里,如果你很好奇: android:onClick XML属性与setOnClickListener有何不同?

However, if you're using onTouch, then you will have to stick with what everyone else is suggesting. 但是,如果您使用onTouch,那么您将不得不坚持其他人的建议。 XML does not support an android:onTouch attribute. XML不支持android:onTouch属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM