Java getIntent.getStringExtra返回null

您所在的位置:网站首页 getintent()getStringExtra Java getIntent.getStringExtra返回null

Java getIntent.getStringExtra返回null

2024-07-17 00:12| 来源: 网络整理| 查看: 265

getIntent一直返回null,我不知道问题出在哪里。我使用putExtra()来存储字符串,并且在另一个活动中使用getIntent.getStringExtra()来调用它。

这是包含putExtra()的活动

代码语言:javascript复制 String lati = tvLatitude.getText().toString(); String lng = tvLongitude.getText().toString(); Intent i = new Intent(this, NoteActivity.class); i.putExtra("lati", lati); i.putExtra("lng", lng);

这是getIntent()的活动

代码语言:javascript复制if (getIntent().getStringExtra("lati") != null && (getIntent().getStringExtra("lng") != null)){ String lati = bundle.getString("lati"); String lng = bundle.getString("lng"); addEntry(title, desc, lati, lng); Intent i_note = new Intent(NoteActivity.this, GoogleMaps.class); startActivity(i_note);

我使用这行代码来检查它是否为null,其中它确实返回null:

代码语言:javascript复制if(getIntent().getStringExtra("lati") == null && (getIntent().getIntExtra("lng", 0) == 0)) { invalid = true; Toast.makeText(getApplicationContext(), "Latitude and longitude is null", Toast.LENGTH_SHORT).show(); }

好的,所以问题是这些值是双精度的,所以现在我想弄清楚如何将双精度转换为字符串。这就是我所拥有的:

代码语言:javascript复制public void onLocationChanged(Location location) { TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude); TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude); // Getting latitude of the current location double latitude = location.getLatitude(); // Getting longitude of the current location double longitude = location.getLongitude(); // Creating a LatLng object for the current location LatLng latLng = new LatLng(latitude, longitude); // Showing the current location in Google Map googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); // Zoom in the Google Map googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); // Setting latitude and longitude in the TextView tv_location tvLatitude.setText("Latitude:" + location.getLatitude()); tvLongitude.setText("Longitude:" + location.getLongitude()); // Storing latitude and longitude to string String lati = tvLatitude.getText().toString(); String lng = tvLongitude.getText().toString(); Intent i = new Intent(this, NoteActivity.class); i.putExtra("lati", lati); i.putExtra("lng", lng);

编辑:所以我将双精度值存储为字符串,这看起来很好,但它仍然返回null。这是新的代码:

代码语言:javascript复制public void onLocationChanged(Location location) { TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude); TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude); // Getting latitude of the current location double latitude = location.getLatitude(); String latitude2 = String.valueOf(latitude); // Getting longitude of the current location double longitude = location.getLongitude(); String longitude2 = String.valueOf(longitude); // Creating a LatLng object for the current location LatLng latLng = new LatLng(latitude, longitude); // Showing the current location in Google Map googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); // Zoom in the Google Map googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); // Setting latitude and longitude in the TextView tv_location tvLatitude.setText("Latitude:" + latitude2); tvLongitude.setText("Longitude:" + longitude2); // Storing latitude and longitude to string String lati = tvLatitude.getText().toString(); String lng = tvLongitude.getText().toString(); Intent i = new Intent(this, NoteActivity.class); i.putExtra("lati", lati); i.putExtra("lng", lng);


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3