我有以下代码打开谷歌地图以获取两个位置之间的方向。代码在我测试的所有手机中运行良好,但它不能在平板电脑中运行。
下面是我的代码:当我点击一个按钮时会调用它(当我点击此按钮时它会在平板电脑中崩溃但在手机上正常运行)
Toast.makeText(getApplicationContext(), "Opening Google Map", 1).show();
SharedPreferences carLocation = getSharedPreferences("CarLocation", Context.MODE_PRIVATE);
if(carLocation.contains("carlatitude")){
//do nothing
} else {
showCurrentStatus = true;
}
AsyncTask<Void, Void, Void> dostuff = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try{
if(mLocationClient.isConnected()){
mLocationClient.disconnect();
}
mLocationClient.connect();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
if(mLocationClient.isConnected()){
mLocationClient.disconnect();
}
};
};dostuff.execute();
我的onConnect如下:
@Override
public void onConnected(Bundle arg0) {
Log.d("MAinActivit","fetching current location");
Location location = mLocationClient.getLastLocation();
double curLat = location.getLatitude();
double curLong = location.getLongitude();
SharedPreferences carLocation = getSharedPreferences("CarLocation", Context.MODE_PRIVATE);
double carLat = Double.parseDouble(carLocation.getString("carlatitude", ""+curLat));
double carLong = Double.parseDouble(carLocation.getString("carlongitude", ""+curLong));
if(showCurrentStatus){
Toast.makeText(getApplicationContext(), "Your current location", 0).show();
showCurrentStatus = false;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+curLat+","+curLong));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}else{
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+curLat+","+curLong+"&daddr="+carLat+","+carLong));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
}