无法将数据从一个活动传递到另一个活动 Android

huangapple go评论77阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月6日 00:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63756299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定