当我尝试在地图上显示某些标记时,我得到“无效的双倍”“”我存储在数据库的纬度和经度中并使用这些信息我尝试在地图上显示标记。
我正在学习本教程:http://www.thaicreate.com/mobile/android-google-map-marker-php-mysql.html
我真的希望有人能指出我正确的方向
主:
public class MainActivity extends AppCompatActivity {
private GoogleMap googleMap;
private Double campo_latitud = 0.00;
private Double campo_longitud = 0.00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ArrayList<HashMap<String, String>> location = null;
String url = "http://www.thaicreate.com/android/getLatLon.php"";
try {
JSONArray data = new JSONArray(getHttpGet(url));
location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("id", c.getString("id"));
map.put("campo_latitud", c.getString("campo_latitud"));
map.put("campo_longitud", c.getString("campo_longitud"));
map.put("campo_categoria", c.getString("campo_categoria"));
location.add(map);
System.out.println("campo_latitud");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
campo_latitud = Double.parseDouble(location.get(0).get("campo_latitud").toString());
System.out.println("campo_latitud");
campo_longitud = Double.parseDouble(location.get(0).get("campo_longitud").toString());
LatLng coordinate = new LatLng(campo_latitud, campo_longitud);
googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));
for (int i = 0; i < location.size(); i++) {
campo_latitud = Double.parseDouble(location.get(i).get("Latitude").toString());
campo_longitud = Double.parseDouble(location.get(i).get("Longitude").toString());
String name = location.get(i).get("LocationName").toString();
MarkerOptions marker = new MarkerOptions().position(new LatLng(campo_latitud, campo_longitud)).title(name);
googleMap.addMarker(marker);
}
}
public static String getHttpGet(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
logcat的:
10-17 23:25:18.650 27527-27527/com.example.dell.mapamarcadoresoct17 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.mapamarcadoresoct17, PID: 27527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.mapamarcadoresoct17/com.example.dell.mapamarcadoresoct17.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.dell.mapamarcadoresoct17.MainActivity.onCreate(MainActivity.java:84)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
NEW LOGCAT:
10-18 00:26:04.290 9108-9108/com.example.dell.mapamarcadoresoct17 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.mapamarcadoresoct17, PID: 9108
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.mapamarcadoresoct17/com.example.dell.mapamarcadoresoct17.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.example.dell.mapamarcadoresoct17.MainActivity.onCreate(MainActivity.java:104)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:2)
您的问题是java.lang.NumberFormatException: Invalid double: ""
。
<强>示例强>
String str = "";
if(!TextUtils.isEmpty(str)){
double d = Double.parseDouble(str);
}
在您的代码中尝试此操作。
if (!TextUtils.isEmpty(location.get(i).get("Latitude").toString()) &&!TextUtils.isEmpty(location.get(i).get("Longitude").toString())){
campo_latitud = Double.parseDouble(location.get(i).get("Latitude").toString());
campo_longitud = Double.parseDouble(location.get(i).get("Longitude").toString());
}
答案 1 :(得分:0)
你的日志说出一切......
引起:java.lang.NumberFormatException:无效的double:“”
这意味着在使用parseDouble()
时,您将null值传递给此方法。