我对这个主题做了很多研究,但找不到合适的解决方案。 我只是有一个api,其中包括学校名称及其纬度,经度。
[
{
"id":1,
"name":"Little Angels Higher Secondary School",
"latitude":27.6514,
"longitude":85.3359
},
{
"id":6,
"name":"Baltimore Secondary School",
"latitude":27.6514,
"longitude":85.3359
}
]
我解析了这个api数据。使用GPStracker类我在我当前位置的谷歌地图上成功添加了一个标记。现在我想在谷歌地图中的所有学校位置添加一个标记,以便我可以知道最近的学校到我曾经打开谷歌地图。这就是我所做的一切,它如何显示空指针异常。
package com.example.user.educationhunt.fragment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.user.educationhunt.R;
import com.example.user.educationhunt.SchoolDetails;
import com.example.user.educationhunt.adapter.CustomListAdapter;
import com.example.user.educationhunt.pojos.AppController;
import com.example.user.educationhunt.pojos.FeeClass;
import com.example.user.educationhunt.pojos.GpsLocation;
import com.example.user.educationhunt.pojos.MySchool;
import com.example.user.educationhunt.pojos.OurSchool;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class NearMe extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
GpsLocation gpsLocation;
double longitude, latitude;
private ProgressDialog pDialog;
private RadioButton radioSexButton;
ArrayList al = new ArrayList();
MySchool mySchool;
Marker place;
LatLng current_location ;
private static final String TAG = NearMe.class.getSimpleName();
private static final String url = "http://www.myeducationhunt.com/api/v1/schools";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_near_me, container, false);
radioSexButton = (RadioButton) v.findViewById(R.id.radioSchool);
if (isConnected()) {
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
gpsLocation = new GpsLocation(getContext());
if (gpsLocation.canGetLocation()) {
longitude = gpsLocation.getLongitude();
latitude = gpsLocation.getLatitude();
Toast.makeText(getContext(), "latitude:" + latitude + "Longitude:" + longitude, Toast.LENGTH_LONG).show();
}
pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Loading…");
pDialog.show();
JsonArrayRequest schoolRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
mySchool = new MySchool();
mySchool.setId(""+obj.getInt("id"));
mySchool.setName(""+obj.getString("name"));
mySchool.setLatitude(Double.parseDouble(""+obj.getDouble("latitude")));
mySchool.setLongitude(Double.parseDouble(""+obj.getDouble("longitude")));
al.add(mySchool);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(schoolRequest);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
LatLng schoollatlng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(schoollatlng).title("MyLocation"));
CameraPosition cameraPosition = new CameraPosition.Builder().target(schoollatlng).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
LatLng latlng = new LatLng(mySchool.getLatitude(), mySchool.getLongitude());
googleMap.addMarker(new MarkerOptions().position(latlng).title(mySchool.getName()));
CameraPosition cameraPosition1 = new CameraPosition.Builder().target(latlng).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition1));
}
});
} else {
Toast.makeText(getContext(), "Please check your internet connection", Toast.LENGTH_LONG).show();
}
return v;
}
public boolean isConnected() {
ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
有没有人帮助我?请帮助。谢谢提前
这是MySchool课程
package com.example.user.educationhunt.pojos;
/**
* Created by user on 12/28/2016.
*/
public class MySchool {
String id;
String name;
double latitude;
double longitude;
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public void setId(String id) {
this.id = id;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setName(String name) {
this.name = name;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
这是我的错误日志
FATAL EXCEPTION: main
Process: com.example.user.educationhunt, PID: 20472
java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.user.educationhunt.pojos.MySchool.getLatitude()' on a null object reference
at com.example.user.educationhunt.fragment.NearMe$3.onMapReady(NearMe.java:159)
at com.google.android.gms.maps.MapView$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:387)
at zu.a(:com.google.android.gms.DynamiteModulesB:82)
at maps.ad.t$5.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
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)
12-28 10:06:05.046 20472-21276/com.example.user.educationhunt I/qtaguid: Untagging socket 81
答案 0 :(得分:3)
您的学校标记应在Google完成初始化后运行。以下是代码示例。
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
LatLng schoollatlng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(schoollatlng).title("MyLocation"));
CameraPosition cameraPosition = new CameraPosition.Builder().target(schoollatlng).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
drawSchoolMarker();
}
});
在班级
中创建新方法private void drawSchoolMarker(){
JsonArrayRequest schoolRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
MySchool school = new MySchool();
school.setId(""+obj.getInt("id"));
school.setName(""+obj.getString("name"));
school.setLatitude(Double.parseDouble(""+obj.getDouble("latitude")));
school.setLongitude(Double.parseDouble(""+obj.getDouble("longitude")));
al.add(school);
} catch (JSONException e) {
e.printStackTrace();
}
}
//iterate from arraylist
for(MySchool school : al){
LatLng latlng = new LatLng(school.getLatitude(), school.getLongitude());
googleMap.addMarker(new MarkerOptions().position(latlng).title(school.getName()));
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(schoolRequest);
}
我没有测试代码但它应该可以工作。
答案 1 :(得分:1)
阅读完所有学校位置之后,请使用您的数组列表
Marker place;
LatLng current_location ;
for(int i = 0; i < al.size(); i++){
current_location = new LatLng(Double.parseDouble(al.get(i).getLatitude()),Double.parseDouble(al.get(i).getLongitude()));
place= map.addMarker(new MarkerOptions().position(current_location).title("school name here"));
}
答案 2 :(得分:0)
你应该在行中获得NullPointerException:
LatLng latlng = new LatLng(ourSchool.latitude, ourSchool.longitude);
因为我们学校指的是Null。您已在ArrayList“al”中添加了所有ourSchool对象,如:al.add(ourSchool)
现在尝试在ArrayList中添加数据后从ArrayList“al”进行迭代。
此外,您的以下代码在从网址获取回复之前运行,而您的“ourSchool”在此之前尚未初始化。
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
LatLng schoollatlng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(schoollatlng).title("MyLocation"));
CameraPosition cameraPosition = new CameraPosition.Builder().target(schoollatlng).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
LatLng latlng = new LatLng(ourSchool.latitude, ourSchool.longitude);
googleMap.addMarker(new MarkerOptions().position(latlng).title(ourSchool.schoolName));
CameraPosition cameraPosition1 = new CameraPosition.Builder().target(latlng).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition1));
}
});
OurSchool课程:
class OurSchool{
String id;
String name;
double latitude;
double longitude;
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setLatitude(double latitude){
this.latitute=latitude;
}
public double getLatitude(){
return latitude;
}
public void setLongitude(String longitude){
this.longitude=longitude;
}
public double getLongitude(){
return longitude;
}
}
然后在OurSchool类对象中设置值,如:
ourSchool.setId(""+obj.getInt("id"));
取代
ourSchool.schoolId = obj.getInt("id");
同样为其他
设置值