我很难从MySQL数据库接收谷歌地图标记。 我有这个问题java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int java.util.ArrayList.size()'。
我的部分代码:
ArrayList<HashMap<String, String>> location = null;
try {
JSONArray data = new JSONArray(RetrieveTask.class);
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("lat", c.getString("lat"));
map.put("lng", c.getString("lng"));
location.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < location.size/*here it shows the problem - size*/(); i++) {
String id = location.get(i).get("id");
double lat = Double.parseDouble(location.get(i).get("lat"));
double lng = Double.parseDouble(location.get(i).get("lng"));
LatLng latLng = new LatLng(lat, lng);
MarkerOptions marker = new MarkerOptions().position(latLng).title(id);
googleMap.addMarker(marker);
}
连接到interenet的代码:
private class RetrieveTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
String strUrl = "http://12345.php";
URL url = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
我不知道我的代码有什么问题,我尝试了不同的方法,但仍然有同样的错误。 以前有没有人在遇到同样的问题,你是怎么解决的?
可能是数据库故障,因为它不是远程的,我使用localhost? JSON响应正常,它返回预期值。
答案 0 :(得分:0)
肯定你有NPE,因为你捕获了JSONException
catch (JSONException e) {
e.printStackTrace();
}
你因为这条线而得到它
JSONArray data = new JSONArray(RetrieveTask.class);
您没有通过此行从互联网上获取数据。您只是尝试从JSONArray
对象创建class
。
您必须在onPostExecute
AsyncTask
protected void onPostExecute(String result) {
//call here method of Activity or parse your JSONArray
JSONArray data = new JSONArray(result);
//your other code
}
答案 1 :(得分:0)
我认为您没有按照正确的程序从localhost检索数据。您首先需要覆盖AsyncTask的 onPostExecute()方法,然后您应该在 onPostExecute()方法中从localhost检索结果数据。
您的代码在此行崩溃
JSONArray data = new JSONArray(RetrieveTask.class);
因此位置尚未初始化。因此,当您尝试在下一个for循环中访问位置时,它会抛出NullPointerException。
代码的外观如何:
private class RetrieveTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
String strUrl = "http://12345.php";
URL url = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
protected String onPostExecute(String result){
ArrayList<HashMap<String, String>> location = null;
try {
JSONArray data = new JSONArray(result);
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("lat", c.getString("lat"));
map.put("lng", c.getString("lng"));
location.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
//the if loop is to check if location is initialised above
if(location!=null){
for (int i = 0; i < location.size/*here it shows the problem - size*/(); i++) {
String id = location.get(i).get("id");
double lat = Double.parseDouble(location.get(i).get("lat"));
double lng = Double.parseDouble(location.get(i).get("lng"));
LatLng latLng = new LatLng(lat, lng);
MarkerOptions marker = new MarkerOptions().position(latLng).title(id);
googleMap.addMarker(marker);
}
}
}
}
然后你可以用这种方式执行AsyncTask:
new RetrieveTask().execute();
这是AsyncTask的一个示例,它可以帮助您了解相同的流量。
You can skip to Step no. 7 to know about the Android setup for connecting to localhost