1

我在我的游戏中添加了一个计时器,但这样做我不断收到错误“View 类型中的方法 setOnTouchListener(View.OnTouchListener) 不适用于参数 (scene5.MyCountDownTimer)”我似乎无法修复它。这可能只是我犯了一个简单的错误,但任何解决方案都会很棒。错误大约在我的代码的一半处,我用//这是我的问题区域标记了它!干杯伙计们

公共类 scene5 扩展 Activity 实现 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 ();
    }
    }
4

3 回答 3

2

你的问题在这里:

public class scene5 extends Activity implements OnClickListener, OnTouchListener {
    ...
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ...
    }
}

您还必须实现OnTouchListener

于 2013-07-20T14:49:41.507 回答
0

利用

 scene5.this 

而不是这个

于 2013-07-20T14:45:05.827 回答
-1

问题是iv.setOnTouchListener (this);

您传递的this上下文属于计时器任务,您需要使用iv.setOnTouchListener (scene5.this);

另外,我认为您需要在其本身中找到ImageView参考。onCreate你目前正在做的事情ImageView iv = (ImageView) findViewById (R.id.image);不会奏效。

于 2013-07-20T14:47:58.090 回答