1

I'm trying to call getX() on a button, but eclipse is telling me there's no such method for button objects. In fact, it's telling me there's no getX() for Views either, which baffles me because the API says there are. Code:

public boolean onTouch(View arg0, MotionEvent arg1) {
    int touchX = (int)arg1.getX();
    int touchY = (int)arg1.getY();
    int tI_1 = (int)I_1.getX();
    switch(touchX){
       case tI_1:
          //do something            
       break;
    }
    return false;
}
wehweh
  • 67
  • 2
  • 14
  • 1
    How are you switching on a float value? (ie - touchX) IIRC, you can't switch over float values. Is this related to you calling getX()? If not, what is the scope in/around of this switch statement? Could we get some surrounding code to determine further what the problem is? – Jack Jul 26 '11 at 03:49
  • Um... why do you have a `float` in your case statement? – trutheality Jul 26 '11 at 03:51
  • My bad on that--it's not the actual code, wjust whatever cnippet I could think of. The issue isn't the type here, I have it cast at some point. The issue is the method call. EDIT: updated code with what I actually had. – wehweh Jul 26 '11 at 03:59

3 Answers3

2

getX() and getY() are available only from API level 11 onwards. Try changing the API level of your application, might work.

Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
0

If getX throws an Exception then use getLeft instead.

View source code (part):

// Added in API level 1
public final int getLeft() {
    return mLeft;
}

// Added in API level 11
public float getX() {
    return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
}
user
  • 1
0

you cant call getX() so directly like that... you should set an onTouchListener and a MotionEvent... try this Get the co-ordinates of a touch event on Android

Community
  • 1
  • 1
Adam Storm
  • 724
  • 1
  • 6
  • 13
  • Updated original post. What I'm trying to do here is compare the touch to where the button is. And, again, according to the API, any View object should have the getX() method. [link](http://developer.android.com/reference/android/view/View.html#getX()) And a button is a View, it should also have the getX() method, which according to the API, it does [link](http://developer.android.com/reference/android/widget/Button.html) – wehweh Jul 26 '11 at 04:08
  • hmm.. Odd. Perhaps you are missing an import? otherwise it seems as if your code is formatted properly... granted that your onTouchListener is declared properly.. Could I see how your onCreate Method? – Adam Storm Jul 26 '11 at 04:31