我的intent.putExtras(无法解析字符串)有什么问题?

时间:2016-04-27 06:34:50

标签: java android google-maps

我需要获取地址并将其传递给谷歌地图。我已将所有数据传递给DisplayItem Activity。现在,从Displayitem活动我只想将地址传递给MapActivity。

这是我成功显示所有数据的活动。之后我创建了一个按钮,以便用户可以转到mapActivity查看位置。

private Bundle extras;

private TextView mallAddress;
private TextView tvMallName;
private TextView state, url, phone_number, mail,wb,pn, town;

ImageView map_Locations;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displayitem);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    tvMallName = (TextView) findViewById(R.id.tv_mallname);
    state = (TextView) findViewById(R.id.states);
    mallAddress = (TextView) findViewById(R.id.mall_Address);

    map_Locations= (ImageView) findViewById(R.id.map_location);



    extras = getIntent().getExtras();
    String malladress = extras.getString(EXTRA_ADDRESS,"");
    String mallName = extras.getString(EXTRA_NAME, "");
    String mallstate = extras.getString(EXTRA_STATE,"");


    mallAddress.setText(malladress);
    tvMallName.setText(mallName);
    state.setText(mallstate);

}

这是我创建的按钮(我的字符串“地址”有问题)我做错了吗?它表示(无法解析字符串,字符串)在intent.putExtras()

public void map(View view){
    map_Locations= (ImageView) findViewById(R.id.map_location);
    map_Locations.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String address = mallAddress.getText().toString();
            Intent intent= new Intent(Displayitem.this, MapsActivity.class);

            intent.putExtras(MapsActivity.EXTRA_ADDRESS, address);
            startActivity(intent);


        }
    });

}

在MapActivity上我检索数据

 private void getAddress(GoogleMap googleMap)  {

   try {
       mMap = googleMap;
       Intent intent = getIntent();
       extras = intent.getExtras();

       Geocoder geocoder = new Geocoder(this);


       address = extras.getString(this.EXTRA_ADDRESS);

       location = geocoder.getFromLocationName(address, 1);
       if (location.size() > 0) {
           lati =location.get(0).getLatitude();
           lon =location.get(0).getLongitude();
           MarkerOptions options = new MarkerOptions()
                   .title(name)
                   .position (new LatLng(lati, lon));
           mMap.addMarker(options);
       }

   }catch (Exception e){
       e.printStackTrace();

   }
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    getAddress(googleMap);
    setUpMap();

}

PS。我新手

1 个答案:

答案 0 :(得分:3)

intent.putExtras(...);

它只是intent.putExtra(...)

intent.putExtra(MapsActivity.EXTRA_ADDRESS, address);

检查official docs