Reputation: 39
I was wanting to be able to have a button which would launch the Google Maps app, however I cannot really seem to be able to get it to work, could I have some help? Really sorry if this seems easy, i'm still a newbie with android, below is my oncreate method with the relavent Intents
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setIcon(R.drawable.ic_action_headphones);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Button mapsbtn = (Button) findViewById(R.id.mapbtn);
mapsbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});
}
My app keeps crashing when the button is pressed, and the error i am receiving is
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: juddsafc.actionbarexample, PID: 2980 java.util.MissingFormatArgumentException: Format specifier: f
Thanks in advance!
Upvotes: 0
Views: 147
Reputation: 354
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
The %f is a formatter. Your program expects a value to replace %f, but there's not enough arguments for your function to accomplish this.
You could do the following:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 100, 200);
And instead of using 100 and 200, you pass along the values you want to display.
Upvotes: 0
Reputation: 3093
When you put %f,%f
you need to provide values afterwards... So change this:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
to this:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 35.6833, -139.7667); // numbers are just some random coordinates
Upvotes: 1