我想在我的应用程序中实现地图。但是如果我运行应用程序并尝试使用地图,它会被强制关闭。这是我的代码。我尝试了所有其他帖子 bt cudnt 解决了问题。请帮助我
package com.org;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class Maps extends MapActivity {
MapView MapView = null;
MapController myMC = null;
GeoPoint geoPoint = null;
MyLocationOverlay myLocationOverlay = null;
private List<Overlay> mapOverlays;
String FR="";
String FR1="";
String FRname = "";
private Projection projection;
private static final String NOTIFY_KEYWORD="Maps";
public static final int NOTIFICATION_ID=1337;
//double latitude = 19.188888, longitude = 72.908333;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapView = (MapView) findViewById(R.id.myGMap);
//geoPoint = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
MapView.setSatellite(false);
myMC = MapView.getController();
// myMC.setCenter(geoPoint);
//myMC.setZoom(15);
/*myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);*/
//myMC = MapView.getController();
myMC.setZoom(MapView.getMaxZoomLevel()-13);
Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
MyItemizedOverlay myItemizedOverlay
= new MyItemizedOverlay(marker, Maps.this);
MapView.getOverlays().add(myItemizedOverlay);
getLocation();
FR = FR.replace("[", "");
FR = FR.replace("]", "");
FR = FR.replace("\"", "");
String lat_arr[] = FR.split(",");
FR1 = FR1.replace("[", "");
FR1 = FR1.replace("]", "");
FR1 = FR1.replace("\"", "");
String long_arr[] = FR1.split(",");
FRname = FRname.replace("[", "");
FRname = FRname.replace("]", "");
FRname = FRname.replace("\"", "");
String name_arr[] = FRname.split(",");
for (int i=0; i<long_arr.length; i++)
{
//GeoPoint myPoint1 = new GeoPoint(Integer.parseInt(lat_arr[i]), Integer.parseInt(long_arr[i]));
myItemizedOverlay.addItem(new GeoPoint(Integer.parseInt(lat_arr[i]), Integer.parseInt(long_arr[i])) , name_arr[i].toString() , name_arr[i].toString());
}
myLocationOverlay = new MyLocationOverlay(this, MapView);
MapView.getOverlays().add(myLocationOverlay);
MapView.postInvalidate();
double distance;
Location locationA = new Location("point A");
locationA.setLatitude(19188888);
locationA.setLongitude(72908333);
Location locationB = new Location("point B");
locationB.setLatitude(17188888);
locationB.setLongitude(80908333);
distance = locationA.distanceTo(locationB);
/* paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText(Double.toString(distance), 10, 25, paint);*/
Toast toast = Toast.makeText(this, Double.toString(distance/1000)+" Km" , Toast.LENGTH_LONG);
toast.show();
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
//Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
//canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
}
@Override
protected boolean isLocationDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_I) {
MapView.getController().setZoom(MapView.getZoomLevel() + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
MapView.getController().setZoom(MapView.getZoomLevel() - 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_S) {
MapView.setSatellite(true);
return true;
} else if (keyCode == KeyEvent.KEYCODE_M) {
MapView.setSatellite(false);
return true;
}
return super.onKeyDown(keyCode, event);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
private void getLocation() {
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString("user", null);
String gid = myPrefs.getString("gid", null);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://182.237.186.79:8080/ubi/getlocation.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("gid", gid.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
JSONObject jsonResponse = new JSONObject(result);
JSONObject object = jsonResponse.getJSONObject("lat");
FR = object.getString("latv");
JSONObject object1 = jsonResponse.getJSONObject("long");
FR1 = object1.getString("longv");
JSONObject object2 = jsonResponse.getJSONObject("name");
FRname = object2.getString("namev");
} catch (Exception e) {
e.printStackTrace();
}
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
这是我的日志猫
04-09 21:11:27.576: W/KeyCharacterMap(323): No keyboard for id 0
04-09 21:11:27.576: W/KeyCharacterMap(323): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-09 21:11:44.126: D/dalvikvm(323): GC_FOR_MALLOC freed 3561 objects / 204176 bytes in 66ms
04-09 21:11:44.356: I/global(323): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-09 21:11:46.656: I/global(323): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-09 21:11:48.706: D/dalvikvm(323): GC_FOR_MALLOC freed 1887 objects / 137264 bytes in 216ms
04-09 21:11:48.848: I/global(323): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-09 21:11:49.291: I/global(323): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-09 21:11:49.351: W/System.err(323): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
04-09 21:11:49.533: W/System.err(323): at org.json.JSON.typeMismatch(JSON.java:107)
04-09 21:11:49.533: W/System.err(323): at org.json.JSONObject.<init>(JSONObject.java:158)
04-09 21:11:49.536: W/System.err(323): at org.json.JSONObject.<init>(JSONObject.java:171)
04-09 21:11:49.536: W/System.err(323): at com.org.groupmain.getmsg(groupmain.java:229)
04-09 21:11:49.536: W/System.err(323): at com.org.groupmain.onCreate(groupmain.java:82)
04-09 21:11:49.536: W/System.err(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 21:11:49.536: W/System.err(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-09 21:11:49.536: W/System.err(323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-09 21:11:49.536: W/System.err(323): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-09 21:11:49.536: W/System.err(323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-09 21:11:49.536: W/System.err(323): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 21:11:49.536: W/System.err(323): at android.os.Looper.loop(Looper.java:123)
04-09 21:11:49.536: W/System.err(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-09 21:11:49.536: W/System.err(323): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:11:49.536: W/System.err(323): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 21:11:49.546: W/System.err(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-09 21:11:49.546: W/System.err(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-09 21:11:49.546: W/System.err(323): at dalvik.system.NativeStart.main(Native Method)
04-09 21:11:52.328: W/KeyCharacterMap(323): No keyboard for id 0
04-09 21:11:52.328: W/KeyCharacterMap(323): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-09 21:11:54.346: D/dalvikvm(323): GC_FOR_MALLOC freed 2919 objects / 604944 bytes in 61ms
04-09 21:11:54.596: D/dalvikvm(323): GC_FOR_MALLOC freed 5593 objects / 685600 bytes in 63ms
04-09 21:11:54.856: D/dalvikvm(323): GC_FOR_MALLOC freed 6413 objects / 440352 bytes in 63ms
04-09 21:11:55.067: D/dalvikvm(323): GC_FOR_MALLOC freed 5745 objects / 372104 bytes in 68ms
04-09 21:11:55.266: D/dalvikvm(323): GC_FOR_MALLOC freed 2279 objects / 138864 bytes in 126ms
04-09 21:11:55.516: D/dalvikvm(323): GC_FOR_MALLOC freed 7914 objects / 618728 bytes in 71ms
04-09 21:11:55.746: D/dalvikvm(323): GC_FOR_MALLOC freed 6116 objects / 370120 bytes in 78ms
04-09 21:11:56.026: D/dalvikvm(323): GC_FOR_MALLOC freed 5880 objects / 446808 bytes in 68ms
04-09 21:11:56.026: I/dalvikvm-heap(323): Grow heap (frag case) to 3.313MB for 87396-byte allocation
04-09 21:11:56.166: D/dalvikvm(323): GC_FOR_MALLOC freed 30 objects / 1800 bytes in 134ms
04-09 21:11:56.296: D/dalvikvm(323): GC_FOR_MALLOC freed 0 objects / 0 bytes in 131ms
04-09 21:11:56.296: I/dalvikvm-heap(323): Grow heap (frag case) to 3.395MB for 87396-byte allocation
04-09 21:11:56.426: D/dalvikvm(323): GC_FOR_MALLOC freed 0 objects / 0 bytes in 127ms
04-09 21:11:56.576: I/global(323): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-09 21:11:56.576: W/System.err(323): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
04-09 21:11:56.586: W/System.err(323): at org.json.JSON.typeMismatch(JSON.java:107)
04-09 21:11:56.586: W/System.err(323): at org.json.JSONObject.<init>(JSONObject.java:158)
04-09 21:11:56.586: W/System.err(323): at org.json.JSONObject.<init>(JSONObject.java:171)
04-09 21:11:56.586: W/System.err(323): at com.org.Maps.getLocation(Maps.java:272)
04-09 21:11:56.586: W/System.err(323): at com.org.Maps.onCreate(Maps.java:101)
04-09 21:11:56.586: W/System.err(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 21:11:56.586: W/System.err(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-09 21:11:56.586: W/System.err(323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-09 21:11:56.586: W/System.err(323): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-09 21:11:56.597: W/System.err(323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-09 21:11:56.597: W/System.err(323): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 21:11:56.597: W/System.err(323): at android.os.Looper.loop(Looper.java:123)
04-09 21:11:56.597: W/System.err(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-09 21:11:56.597: W/System.err(323): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:11:56.597: W/System.err(323): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 21:11:56.597: W/System.err(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-09 21:11:56.597: W/System.err(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-09 21:11:56.597: W/System.err(323): at dalvik.system.NativeStart.main(Native Method)
04-09 21:11:56.606: D/AndroidRuntime(323): Shutting down VM
04-09 21:11:56.606: W/dalvikvm(323): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-09 21:11:56.616: E/AndroidRuntime(323): FATAL EXCEPTION: main
04-09 21:11:56.616: E/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org/com.org.Maps}: java.lang.NumberFormatException: unable to parse '' as integer
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-09 21:11:56.616: E/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:11:56.616: E/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 21:11:56.616: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-09 21:11:56.616: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-09 21:11:56.616: E/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
04-09 21:11:56.616: E/AndroidRuntime(323): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
04-09 21:11:56.616: E/AndroidRuntime(323): at java.lang.Integer.parseInt(Integer.java:412)
04-09 21:11:56.616: E/AndroidRuntime(323): at java.lang.Integer.parseInt(Integer.java:382)
04-09 21:11:56.616: E/AndroidRuntime(323): at com.org.Maps.onCreate(Maps.java:118)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 21:11:56.616: E/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-09 21:11:56.616: E/AndroidRuntime(323): ... 11 more
04-09 21:12:02.626: I/Process(323): Sending signal. PID: 323 SIG: 9