从我的 Android 应用打开 Google 地图

Opening Google Maps from my Android app

我希望能够有一个启动 Google 地图应用程序的按钮,但我似乎真的无法让它工作,我能得到一些帮助吗?真的很抱歉,如果这看起来很简单,我仍然是 android 的新手,下面是我的 oncreate 方法和相关的 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);
        }
    });

}

我的应用程序在按下按钮时一直崩溃,我收到的错误是

E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: juddsafc.actionbarexample, PID: 2980 java.util.MissingFormatArgumentException: Format specifier: f

提前致谢!

当你输入 %f,%f 之后你需要提供值...所以改变这个:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f");

对此:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 35.6833, -139.7667);  // numbers are just some random coordinates
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");

%f 是格式化程序。您的程序需要一个值来替换 %f,但是您的函数没有足够的参数来完成此操作。

您可以执行以下操作:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 100, 200);

而不是使用 100 和 200,您传递要显示的值。