[英]Getting null when trying to access variables from another class, in Java
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. 我的第一个类名为Session,第二个名为 - 它们位于同一个包中。
I declare the variables as public in the main class. 我在主类中声明变量为public。 On touch, I save the users current location into the (public) geo-point variable called userposition . 在触摸时,我将用户当前位置保存到名为userposition的(公共)地理点变量中。
Then in the second class I use 然后在我使用的第二课
Session pass = new Session();
to create an instance of the Session. 创建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?? 但它继续返回null并抛出异常,即使我在主类上测试变量并且工作得很好......我在这里缺少什么?
The main code for Session is: Session的主要代码是:
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. 我注意到的(我不太擅长Java所以我会尝试解释)是我可以从主类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. 问题是,为了传递地理点,我需要将变量传递到Session内的Gesture Detection onDoubleTap方法...类似于pass.onDoubleTap.userposition,因为这是我需要的变量,并且在该类中变量不是空的。 But how I go about doing that?? 但是我怎么做呢?
我通过声明变量static
来解决它,以便它可以在包中的所有类之间共享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.