在下面的代码中,我得到了NullPointerException。我知道这意味着我在第228行使用的东西之一是null,而不是有效地址。但我不明白为什么。我错过了什么吗?
public class MainActivity extends ActionBarActivity {
LatLng xmyLL2;
private Marker marker2;
protected void onCreate(Bundle savedInstanceState) {
marker2 = googleMap.addMarker(new MarkerOptions().
position(xmyLL2).title("ver2"));
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
double xd2 = Double.parseDouble(xstr[2]);
double xd3 = Double.parseDouble(xstr[3]);
xmyLL2 = new LatLng( xd2, xd3 );
marker2.setPosition(xmyLL2); // this is line228
}
}在line 228: marker2.setPosition(xmyLL2);上,我得到了异常,这是堆栈跟踪:
MainActivity$MyLocationListener.onLocationChanged(Location) line: 228
LocationManager$ListenerTransport._handleMessage(Message) line: 279
LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 208发布于 2014-11-17 03:24:14
onLocationChanged()似乎是在onCreate()之前被调用的。
如果您控制了这些,请确保首先调用onCreate。如果您没有控制权,您可以使用if null以避免异常,并希望在onCreate完成其神奇操作后,稍后再次调用它。
将行更改为:
if (null != marker2) marker2.setPosition(xmyLL2); // this is line228https://stackoverflow.com/questions/26964857
复制相似问题