我试图在我的应用程序中创建一个按钮,在特定位置打开谷歌地图应用程序。我一直在尝试使用 URI 和意图。当我尝试启动活动时,应用程序意外退出。这只发生在我使用 geo:xx,xx 我能够打开一个网址,但不能打开谷歌地图应用程序时。我不太确定这里有什么问题,但这里是我正在使用的按钮的 onclick 方法:
mapButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
String UriString = getResources().getString(R.string.map_location);
Uri geoUri = Uri.parse(UriString);
Intent mapCall = new Intent(Intent.ACTION_VIEW, geoUri);
startActivity(mapCall);
}
});
我觉得这应该很简单,但它对我不起作用。任何帮助,将不胜感激!
如果有帮助,这是我迄今为止编写的其余代码:
public class MainActivity extends Activity implements OnClickListener{
public Button courseButton;
public Button mapButton;
public Button newsButton;
public Button directoryButton;
public Button calendarButton;
public Button videoButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapButton = (Button)findViewById(R.id.mapButton);
courseButton = (Button)findViewById(R.id.courseButton);
newsButton = (Button)findViewById(R.id.newsButton);
directoryButton = (Button)findViewById(R.id.directoryButton);
calendarButton = (Button)findViewById(R.id.calendarButton);
videoButton = (Button)findViewById(R.id.videoButton);
courseButton.setOnClickListener(this);
mapButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
String UriString = getResources().getString(R.string.map_location);
Uri geoUri = Uri.parse(UriString);
Intent mapCall = new Intent(Intent.ACTION_VIEW, geoUri);
startActivity(mapCall);
}
});
newsButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String UriString = getResources().getString(R.string.rss_feed);
Uri rssUri = Uri.parse(UriString);
Intent rssCall = new Intent(Intent.ACTION_VIEW, rssUri);
startActivity(rssCall);
}
});
directoryButton.setOnClickListener(this);
calendarButton.setOnClickListener(this);
videoButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
我在这里真的很茫然。
按照要求:
10-29 23:02:25.031: E/AndroidRuntime(324): FATAL EXCEPTION: main
10-29 23:02:25.031: E/AndroidRuntime(324): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=geo:44.873799766954136, -91.92715644836426?z=22 }
10-29 23:02:25.031: E/AndroidRuntime(324): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.app.Activity.startActivityForResult(Activity.java:2827)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.app.Activity.startActivity(Activity.java:2933)
10-29 23:02:25.031: E/AndroidRuntime(324): at com.example.stout.MainActivity$1.onClick(MainActivity.java:46)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.view.View.performClick(View.java:2485)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.view.View$PerformClick.run(View.java:9080)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.os.Handler.handleCallback(Handler.java:587)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.os.Looper.loop(Looper.java:123)
10-29 23:02:25.031: E/AndroidRuntime(324): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-29 23:02:25.031: E/AndroidRuntime(324): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 23:02:25.031: E/AndroidRuntime(324): at java.lang.reflect.Method.invoke(Method.java:507)
10-29 23:02:25.031: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-29 23:02:25.031: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-29 23:02:25.031: E/AndroidRuntime(324): at dalvik.system.NativeStart.main(Native Method)