Intent extras在我的应用程序中返回null

时间:2018-06-18 23:22:54

标签: android android-intent extra

我想将地图类中地图的位置信息解析为另一个ActivitysetText() EditText,但我的额外回报null。 首先,我点击第二课中的按钮然后我去地图活动中的地图。最后,我想解析从地图到第二个活动的位置。

地图类:

save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        LatLng location= mMarker.getPosition();
        double longitude= location.longitude;
        double latitude=location.latitude;
        List<Address> addressList = new ArrayList<>();
        try{
            addressList = geocoder.getFromLocation(latitude , longitude , 1);
            String adresse = addressList.get(0).getAddressLine(0);
            String country = addressList.get(0).getCountryName();
            String fulladr = adresse +" , "+country;
            Toast.makeText(MapsActivity.this, fulladr, Toast.LENGTH_SHORT).show();
            Intent i = new Intent(MapsActivity.this,Partierendezvous.class);
            i.putExtra("result",fulladr.toString().trim());
            setResult(Partierendezvous.RESULT_OK,i);
            finish();

和第二类:

Bundle extras = getIntent().getExtras();
if(extras != null) {
    String value = extras.getString("result");
    edlieurend.setText(value);
} else {
    Toast.makeText(getApplicationContext() , "Lieu not found " , Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:0)

您的问题可能会从更多细节中受益。如果您要将地图活动的结果投放到第二个活动,则需要使用startActivityForResult启动地图活动。

第二项活动

private static final int PICK_LOCATION_REQUEST = 1;

private void pickLocation() {
    .
    .
    startActivityForResult(mapsIntent, PICK_LOCATION_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_LOCATION_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a location.
            // The Intent's data should contain the location you set before finishing the Maps Activity
            String value = data.getString("result");
            edlieurend.setText(value);
        } else {
            Toast.makeText(getApplicationContext() , "Lieu not found " , Toast.LENGTH_SHORT).show();
        }
    }
}