I am making an app in android and i am using google maps.
I have a navigation screen where users can put in a location.
then they click on a button and a mapview opens.
Here the location which they have entered should be navigated too.
When i run this code i get a nullpointerexception for plaats in my map.class
My Navigation code is:
public class Navigatie extends Activity{
public String plaats;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigatie);
EditText text = (EditText)findViewById(R.id.title);
this.plaats = text.getText().toString();
// We create a new ImageButton which links to the map.java class
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);
// We give a onclicklistener method to the button imageButton
imageButton.setOnClickListener(new OnClickListener() {
// if the imageButton is clicked we start a new activity called "Map"
public void onClick(View v) {
startActivity(new Intent(Navigatie.this, Map.class));
}
});
}
}
The code for my maps where the location display is coded, is: (just a part of the code)
public Navigatie nav;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
String plaats = nav.plaats;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(plaats, 5);
String strCompleteAddress = "";
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mc.animateTo(p);
mapView.invalidate();
}
} catch (IOException e) {
e.printStackTrace();
}
Add plaats to your intent, which starts the map using
intent.putExtra("location", this.plaats);
Then in you map activity, in oncreate:
String map_plaats = this.getIntent().getStringExtra("location");
You want to send string to other activity it seems.
For this you can use putExtra("key", "value");
and
you can get this string in Map.class like this
getIntent().getStringExtra("key");
Study here to know more about Intents
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.