0
您好我正在嘗試使用分項疊加在地圖上繪製經度和緯度的數組。我收到以下錯誤。可能是什麼問題..?在Android中使用分項疊加時的空指針
>
03-18 23:52:06.834: W/dalvikvm(2949): threadid=1: thread exiting with uncaught exception (group=0x40b01428)
03-18 23:52:06.844: E/AndroidRuntime(2949): FATAL EXCEPTION: main
03-18 23:52:06.844: E/AndroidRuntime(2949): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rahul.besttracker/com.rahul.besttracker.Mapview}: java.lang.NullPointerException
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.ActivityThread.access$600(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.os.Handler.dispatchMessage(Handler.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.os.Looper.loop(Looper.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.ActivityThread.main(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 23:52:06.844: E/AndroidRuntime(2949): at java.lang.reflect.Method.invoke(Method.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at dalvik.system.NativeStart.main(Native Method)
03-18 23:52:06.844: E/AndroidRuntime(2949): Caused by: java.lang.NullPointerException
03-18 23:52:06.844: E/AndroidRuntime(2949): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at com.rahul.besttracker.HelloItemizedOverlay.addOverlay(HelloItemizedOverlay.java:37)
03-18 23:52:06.844: E/AndroidRuntime(2949): at com.rahul.besttracker.Mapview.onCreate(Mapview.java:56)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.Activity.performCreate(Activity.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-18 23:52:06.844: E/AndroidRuntime(2949): ... 12 more
03-18 23:52:07.935: D/Process(2949): killProcess, pid=2949
這裏是我的HelloItemizedOverlay類
package com.rahul.besttracker;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate(); <--------------GETTING ERROR HERE
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
,這裏是我的MapView類
package com.rahul.besttracker;
import java.util.ArrayList;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class Mapview extends MapActivity {
MapView map;
ArrayList<String> mArrayList;
double[] Lat;
double[] Lon;
String stops[];
int latarr[], lonarr[];
OverlayItem overlayitem;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
map = (MapView) findViewById(R.id.Map);
// map.setBuiltInZoomControls(true);
// setContentView(R.layout.maps);
Bundle gotBasket = getIntent().getExtras();
mArrayList = gotBasket.getStringArrayList("stops");
Lat = gotBasket.getDoubleArray("Lat");
Lon = gotBasket.getDoubleArray("Lon");
System.out.println("SIZE+++" + Lat.length + " " + Lon.length);
for (int i = 0; i < mArrayList.size(); i++) {
System.out.println(" \n" + Lat[i] + "\t" + Lon[i]);
}
// stops=(String[]) mArrayList.toArray();
List<Overlay> mapOverlays = map.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.mark_blue);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
for (int i = 0; i < mArrayList.size(); i++) {
if (Lat[i] > 0 && Lon[i] > 0) {
GeoPoint point = new GeoPoint((int) (Lat[i] * 1E6),
(int) (Lon[i] * 1E6));
overlayitem = new OverlayItem(point, mArrayList.get(i), null);
}
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
可以請你解釋..我沒有得到你的陳述..反駁.... – 2013-03-18 18:47:50
YOur for循環在onCreate只是設置一個if語句中的變量overlayitem的有效值,但你使用它之後if語句在它之外。如果該條件爲false,則意味着overlayitem爲null。但是你仍然把它作爲地圖的一個標記。這會導致最終的空指針錯誤。 – 2013-03-18 18:50:45
好的..謝謝很多好友..那麼工作... – 2013-03-18 18:53:52