我添加了一個計時器到我的遊戲,但這樣做我不斷收到錯誤「類型View中的方法setOnTouchListener(View.OnTouchListener)不適用於參數(scene5.MyCountDownTimer)」我不能似乎修復它。這可能只是我犯了一個簡單的錯誤,但任何解決方案都會很棒。該錯誤是關於我的代碼的一半,我已經標記爲/ /這是我的問題區域!乾杯傢伙setOnTouchListener問題
公共類添加標題SCENE5擴展活動實現OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 20 * 1000;
private final long interval = 1 * 1000;
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("FindIt")
.setMessage("Exit to main menu?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level5);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime/1000));
}
@Override
public void onClick(View v) {
startB.setVisibility(View.INVISIBLE);
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
Intent intent = new Intent(scene5.this, timeUp.class);
scene5.this.startActivity(intent);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
}
{
ImageView iv = (ImageView) findViewById (R.id.image);
if (iv != null) {
iv.setOnTouchListener (this); //THIS IS MY PROBLEM AREA!!
}
toast ("20 seconds to find IT!");}
}
public boolean onTouch (View v, MotionEvent ev)
{
boolean handledHere = false;
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
int nextImage = -1;
ImageView imageView = (ImageView) v.findViewById (R.id.image);
if (imageView == null) return false;
Integer tagNum = (Integer) imageView.getTag();
int currentResource = (tagNum == null) ? R.drawable.levels : tagNum.intValue();
switch (action) {
case MotionEvent.ACTION_DOWN :
if (currentResource == R.drawable.levels) {
nextImage = R.drawable.wrong5;
handledHere = true;
} else handledHere = true;
break;
case MotionEvent.ACTION_UP :
int touchColor = getHotspotColor (R.id.image_areas, evX, evY);
ColorTool ct = new ColorTool();
int tolerance = 25;
nextImage = R.drawable.levels;
if (ct.closeMatch (Color.RED, touchColor, tolerance)){
Intent intent = new Intent(scene5.this, LevelComplete.class);
scene5.this.startActivity(intent);
}
if (currentResource == nextImage) {
nextImage = R.drawable.levels;
}
handledHere = true;
break;
default:
handledHere = false;
}
if (handledHere) {
if (nextImage > 0) {
imageView.setImageResource (nextImage);
imageView.setTag (nextImage);
}
}
return handledHere;
}
public int getHotspotColor (int hotspotId, int x, int y) {
ImageView img = (ImageView) findViewById (hotspotId);
if (img == null) {
Log.d ("ImageAreasActivity", "Hot spot image not found");
return 0;
} else {
img.setDrawingCacheEnabled(true);
Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache());
if (hotspots == null) {
Log.d ("ImageAreasActivity", "Hot spot bitmap was not created");
return 0;
} else {
img.setDrawingCacheEnabled(false);
return hotspots.getPixel(x, y);
}
}
}
public void toast (String msg)
{
Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
感謝JaAd,我覺得有點愚蠢,因爲沒有注意到:)它是漫長的一天。感謝所有解答我問題的人。接受答案。 – user2150591
@ user2150591很高興幫助你,如果它可以幫助你更多,對我來說也是漫長的一天XD! – JJ86