我有这段代码:
Location one = null;
Location two = null;
float dystans = 0;
public void onLocationChanged(Location location) {
if(MainActivity.running && MainActivity.x == 0){
one = location;
MainActivity.x = 1;
}
else if(MainActivity.running && MainActivity.x == 1){
two = location;
dystans = dystans + two.distanceTo(one);
MainActivity.kilometry.setText(Float.toString(dystans));
one = two;
}
}
这个错误:
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我不知道为什么有null,我认为不应该为null,因为onLocationChanged中的位置永远不会为空,所以“dystans”也不能为空
答案 0 :(得分:0)
MainActivity.kilometry是错误所讨论的空引用,而不是dystans。