我试图将一个活动的latlng值转换为另一个活动的latlng值,但是在另一个活动上接收到空值。
活动1
PlacesClient placesClient = Places.createClient(this);
final AutocompleteSupportFragment autocompleteSupportFragment= (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID,Place.Field.LAT_LNG, Place.Field.NAME));
autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
final LatLng latLng= place.getLatLng();
Toast.makeText(searchActivity.this, "Latitude"+latLng.latitude+ "" +"Longitude"+latLng.longitude, Toast.LENGTH_SHORT).show();
//
lat1 = latLng.latitude;
long1 = latLng.longitude;
Intent intent1= new Intent(getApplicationContext(), shortestRouteActivity.class);
intent1.putExtra("lat1",lat1 ); intent1.putExtra("long1",long1);
Log.i("Place Coordinates: " ,"Latitude :"+latLng.latitude+ "" +"Longitude :"+latLng.longitude);
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i( "An error occurred: " , ""+status);
}
});
活动2
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shortest_route);
Bundle bundle = getIntent().getExtras();
double lat1 = bundle.getDouble("lat1");
double long1 = bundle.getDouble("long1");
Log.i("Lat 1 and Long 1 =", lat1 + " : " +long1);
}
错误
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.os.BaseBundle.getDouble(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
我尝试了所有其他方法来获取值,但仍然无法做到这一点。
感谢您的帮助!谢谢你。
1条答案
按热度按时间hmtdttj41#
你如何初始化第二个活动?你的孩子呢
也许你是在获得这些值之前创建第二个活动。