-2
GoogleMap
にNullPointerException
を追加しました。を追加しました。
この問題を解決するのを手伝ってください。GoogleMap.addMarkerエラーNullPointerException
エラー:
08-10 12:49:20.787 6670-6670/com.example.taxiproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.taxiproject, PID: 6670
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
at com.example.taxiproject.DriverPositionActivity$GetLocations.onPostExecute(DriverPositionActivity.java:523)
at com.example.taxiproject.DriverPositionActivity$GetLocations.onPostExecute(DriverPositionActivity.java:437)
これでDriverPositionActivity.java:523
ラインコード:
markers=new HashMap<Marker, Driver>();
removeMarkers();
for (int i = 0; i < drivers.size(); i++) {
MarkerOptions mark=new MarkerOptions();
mark.position(new LatLng(Double.parseDouble(drivers.get(i).getLatitude()),Double.parseDouble(drivers.get(i).getLongitude())));
mark.title(drivers.get(i).getName());
mark.icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi));
//mark.snippet(drivers.get(i).getInfo()+","+drivers.get(i).getCost()+" Rs.pKm, Ph. "+drivers.get(i).getNumber());
-----------------> Marker m=googleMap.addMarker(mark);
mapMarker.add(m);
markers.put(m, drivers.get(i));
}
これでDriverPositionActivity.java:437
ラインコード:あなたはgoogleMap
オブジェクトを宣言していなかった
class GetLocations extends AsyncTask<String, String, String> {
ProgressDialog pDialog;
String toastText="Internet Problem";
String regiresult="";
int success=0;
int error=0;
String errmsg="Server is down";
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog=new ProgressDialog(con);
pDialog.setMessage("Updating drivers locations. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... para) {
List<NameValuePair> params=new ArrayList<NameValuePair>();
JSONObject json=jparser.makeHttpRequest(getDataUrl, "POST", params);
try {
success=json.getInt("success");
if(success==1){
drivers=new ArrayList<Driver>();
JSONArray sounds=json.getJSONArray("location");
for (int i = 0; i < sounds.length(); i++) {
JSONObject jobj=sounds.getJSONObject(i);
Driver d=new Driver();
d.setId(jobj.getString("id"));
d.setName(jobj.getString("name"));
d.setEmail(jobj.getString("email"));
d.setNumber(jobj.getString("number"));
d.setLatitude(jobj.getString("latitude"));
d.setLongitude(jobj.getString("longitude"));
d.setInfo(jobj.getString("info"));
d.setCost(jobj.getString("cost"));
drivers.add(d);
}
}
} catch (JSONException e) {
e.printStackTrace();
error=1;
}catch (Exception e) {
error=1;
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Toast.makeText(MainActivity.this, s+" "+result, Toast.LENGTH_SHORT).show();
pDialog.dismiss();
if(error==1){
if(Util.isConnectingToInternet(con)){
Toast.makeText(con,"Server is down. Please try again", Toast.LENGTH_SHORT).show();
}else{
Util.showNoInternetDialog(con);
}
return;
}
if(success==0){
Toast.makeText(con,"Data loading failed", Toast.LENGTH_SHORT).show();
}else if (success==1){
markers=new HashMap<Marker, Driver>();
removeMarkers();
for (int i = 0; i < drivers.size(); i++) {
MarkerOptions mark=new MarkerOptions();
mark.position(new LatLng(Double.parseDouble(drivers.get(i).getLatitude()),Double.parseDouble(drivers.get(i).getLongitude())));
mark.title(drivers.get(i).getName());
mark.icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi));
//mark.snippet(drivers.get(i).getInfo()+","+drivers.get(i).getCost()+" Rs.pKm, Ph. "+drivers.get(i).getNumber());
Marker m=googleMap.addMarker(mark);
mapMarker.add(m);
markers.put(m, drivers.get(i));
}
scheduleThread();
}
}
}
こんにちは私はプライベートGoogleMap googleMapとしてオブジェクトを宣言しました。活動クラスでこの問題を解決するために私に助言してください。ありがとうございます – selvam