【问题标题】:Opening Google Maps from my Android app从我的 Android 应用程序打开 Google 地图
【发布时间】:2015-02-18 05:40:05
【问题描述】:

我希望能够拥有一个可以启动 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 进程:juddsafc.actionbarexample,PID:2980 java.util.MissingFormatArgumentException:格式说明符:f

提前致谢!

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    当您输入%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
    

    【讨论】:

    • 天哪!,真不敢相信我错过了 :(,非常感谢!
    【解决方案2】:
    String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
    

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

    您可以执行以下操作:

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

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

    【讨论】:

    • 非常感谢您的回答! :),我不敢相信我犯了那个错误:(
    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多