我是一個相對較新的Java/Android編程初學者。我一直在努力,當我在應用程序中按下給定按鈕時,它會產生DTMF音調,但是當我嘗試使用setOnTouchListener時,Android Studio會向我顯示該錯誤。這也讓我對MotionEvent的錯誤,指出表達預計無法解析符號'setOnClickListener' - AndroidStudio
這裏是代碼的重要部分:
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;
}
});
Sushrita,我應該在哪裏把'實現View.OnTouchListener'? 謝謝! :) – alexvandv
在你的課堂開始像這樣:'public class MainActivity extends ActionBarActivity implements View.OnTouchListener {' – Sushrita