简体   繁体   中英

Getter function not returning value when accessed from another class

I have a class as follows:

public class Element extends Activity
{
public float mX;
    public float mY;
 public void animate(long elapsedTime) 
    {
        mX += mSpeedX * (elapsedTime / 20f);
        mY += mSpeedY * (elapsedTime / 20f);
        setmX(mX);
        setmY(mY);
        checkBorders();
    }
public void setmX(float mX) 
{
Log.i("this.mX","mY at setmX read is :"+this.mX );      **//Line 1**
        this.mX = mX;
    }
public float getmX() {
        Log.i("mX","mX in getmX read is :"+mX );    **//Line 2**
        return mX;
    }

    public void setmY(float mY) {

        this.mY = mY;
        Log.i("this.mY","mY at setmY read is :"+this.mY );  **//Line 3**
    }

    public float getmY() {
        Log.i("mY","mY in getY read is :"+mY );    **//Line 4**

        return mY;
    }
}

I have another class

public class Panel extends SurfaceView implements SurfaceHolder.Callback
{
int x = 100;
    int y = 0;
public float xval;
    public float yval; 
@Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        Element element = new Element();
        float x = event.getX();
        float y = event.getY();
        Log.i("x","x in panel is :"+x);
        //toast tos = new toast();

        xval = element.getmX();
        Log.i("xval","xval in playactivity obtained is :"+xval );   **//Line 5**
        yval = element.getmY();
        Log.i("yval","yval in playactivity obtained is :"+xval );    **//Line 6**
return super.onTouchEvent(event);
}

Lines 2, 4, 5, and 6 are showing values as zero. Which I don't want to. Below is the logcat image. 在此输入图像描述

Have I made error in the access Specifiers?

It's hard for us to tell what's going on without knowing mSpeedX and mSpeedY . Try casting both elapsedTime and your mSpeedX/Y variables to floats before doing the math. Floating point math is pretty picky in Java. Also ensure that the animate method is actually being called.

One final note, I probably shouldn't bring this up but it's killing me. You should really, really work on your code consistency and style. Your naming conventions, newline/sameline brace conventions, and indentation habits are all over the place. Code readability and maintainability go down dramatically if your programming style is not consistent .

One more thing: you don't need to call the setter methods after assigning values to a variable. You've already set the value of the very same variable that your set/get methods are accessing. Good luck!

  • You should not create an object of Activity class (in you class Element class) in another class. Please go through the Application fundamentals
  • Though you are creating an object of Element class in the Panel class, you never called setter method setmX() and setmY() to set the values. You are directly calling the getter methods which returns you the default values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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