英文:
Unable to get data from one activity to another activity Android
问题
Activity 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);
    }
});
Activity 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);
}
Error
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)
I have tried all the other ways to get the values but still unable to do so.
Any kind of help is appreciated! Thank you.
英文:
I am trying to the LatLng values from one activity to another, however I'm receiving null values on the other activity.
Activity 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);
        }
    });
Activity 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);
}
Error
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)
I have tried all the other ways to the get the values but still unable to do so.
Any-kind of help is appreciated! Thankyou.
答案1
得分: 1
如何初始化第二个活动?你的 startActivity(intent1); 在哪里?
也许在获取这些值之前你已经创建了第二个活动。
英文:
How are u initializing the 2nd activity? Where is your
startActivity(intent1);
Maybe you are creating the 2nd activity before getting those values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论