3

I have two classes and I want to pass a (geo-point) variable on touch from one to the other, so that I can calculate the distance between user current location and the geo-point.

Long story short:

My first class is named Session and the second Calculation - they are in the same package.

I declare the variables as public in the main class. On touch, I save the users current location into the (public) geo-point variable called userposition.

Then in the second class I use

Session pass = new Session();

to create an instance of the Session. After which I try to retrieve the userposition variable:

Geopoint position = pass.userposition;

But it keeps returning null and throws exception even if I tested the variable on the main class and works great ... What am I missing here??

The main code for Session is:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);
    // Blah blah blah

    // And here i declare the GestureDetector...I working with google maps.
    Gest = new GestureDetector(new SimpleOnGestureListener());

    mapView.getOverlays().add(new Overlay() {
        @Override
        public boolean onTouchEvent(MotionEvent e, MapView mapView) {
            Gest.onTouchEvent(e);
            return super.onTouchEvent(e, mapView);
        }
    });

    Gest.setOnDoubleTapListener(new OnDoubleTapListener() {

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // and in here i set the variable which works fine i tested it
            // in a textView
            userposition = myLocationOverlay.getMyLocation();
        }
    });
}

And for my Calculation class:

public Overlays(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlays.size();
}

@Override
public boolean onTap(int index) {
    // and here i try to get the variable by taping a marker on map (its a
    // lot of code no need to post works fine too)

    Session pass = new Session();
    Geopoint position = pass.userposition;
}

And then I get the null pointer exception.

What I noticed (I'm not so good at Java so I will try to explain) is that I can pass a variable like an integer (I tried it) from the main class Session. The thing is that to pass the geo-point, I need to pass the variable into the Gesture Detection onDoubleTap method inside the Session ... something like pass.onDoubleTap.userposition because that is the variable I need, and in that class the variable is not null. But how I go about doing that??

4
  • Main method is static? so maybe the variable is static? can you post code? Commented Aug 4, 2011 at 15:12
  • is userposition a static variable? If it's an instance variable then try using getter/setter to set/get the value. Also, could you please post your code? Commented Aug 4, 2011 at 15:14
  • That doesn't seem to work....somehow the variable is hidden in the statement and its real value is hidden to other classes... Commented Aug 4, 2011 at 16:00
  • i mean in the gesture detector double tap listener Commented Aug 4, 2011 at 16:04

1 Answer 1

9

I solved it by declaring the variable static, so that it can be shared across all classes in the package.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.