我在地图上编写了显示balloonView
的程序,当我运行程序时,程序已停止。我读了LogCat并获得了NullPointerException
,但我不知道问题出在哪里以及哪一行。
location.java
public class Location extends MapActivity {
private FrameLayout linear;
private MapView map;
private MapController controller;
private EditText text_location;
GeoPoint searchLocation;
GeoPoint mylocation;
double myLatitude=0;
double myLongitude=0;
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
initMapView();
//initZoomControls();
initMyLocation();
//Not complete this method
text_location.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View view, int keyCode,KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_ENTER){
// The map should shows a location that user writes in edittex.
Geocoder geo=new Geocoder(getApplicationContext(),Locale.getDefault());
List<Address> address;
try {
address = geo.getFromLocationName(text_location.getText().toString(),3);
if(address.size()>0){
searchLocation=new GeoPoint((int)(address.get(0).getLatitude()*1e6),(int)(address.get(0).getLongitude()*1e6));
controller.animateTo(searchLocation);
controller.setZoom(12);
//
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
return false;
}
});
mapOverlays=map.getOverlays();
drawable=getResources().getDrawable(R.drawable.marker);
itemizedOverlay=new SimpleItemizedOverlay(drawable,map);
OverlayItem overlayItem=new OverlayItem(searchLocation,text_location.getText().toString(),"for test");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private void initMapView() {
linear = (FrameLayout) findViewById(R.id.frame);
text_location=(EditText)findViewById(R.id.edit_text);
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(true);
}
/*private void initZoomControls() {
//WHY???
View zoomControls = map.getZoomControls();
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL + Gravity.CENTER_HORIZONTAL);
linear.addView(zoomControls, p);
}*/
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass();
//
mylocation=overlay.getMyLocation();
overlay.runOnFirstFix(new Runnable() {
public void run() {
controller.setZoom(13);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
//myLatitude=(overlay.getMyLocation().getLatitudeE6())/1e6;
//myLongitude=(overlay.getMyLocation().getLongitudeE6())/1e6;
}
}
logcat的:
11-09 13:02:37.480: D/libEGL(26632): loaded /system/lib/egl/libGLES_android.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
11-09 13:02:37.496: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
11-09 13:02:37.637: D/OpenGLRenderer(26632): Enabling debug mode 0
11-09 13:02:40.582: D/dalvikvm(26632): GC_CONCURRENT freed 189K, 2% free 14334K/14599K, paused 3ms+2ms
11-09 13:02:40.582: W/CursorWrapperInner(26632): Cursor finalized without prior close()
11-09 13:02:40.691: I/Maps.MyLocationOverlay(26632): Request updates from network
11-09 13:02:40.707: D/dalvikvm(26632): GC_CONCURRENT freed 121K, 2% free 14613K/14855K, paused 2ms+2ms
11-09 13:02:40.855: D/AndroidRuntime(26632): Shutting down VM
11-09 13:02:40.855: W/dalvikvm(26632): threadid=1: thread exiting with uncaught exception (group=0x40a3f1f8)
11-09 13:02:40.855: E/AndroidRuntime(26632): FATAL EXCEPTION: main
11-09 13:02:40.855: E/AndroidRuntime(26632): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.os.Looper.loop(Looper.java:137)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-09 13:02:40.855: E/AndroidRuntime(26632): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:02:40.855: E/AndroidRuntime(26632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 13:02:40.855: E/AndroidRuntime(26632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 13:02:40.855: E/AndroidRuntime(26632): at dalvik.system.NativeStart.main(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632): Caused by: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
11-09 13:02:40.855: E/AndroidRuntime(26632): at org.example.loyaltier.SimpleItemizedOverlay.addOverlay(SimpleItemizedOverlay.java:27)
11-09 13:02:40.855: E/AndroidRuntime(26632): at org.example.loyaltier.Location.onCreate(Location.java:85)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.Activity.performCreate (Activity.java:4465)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-09 13:02:40.855: E/AndroidRuntime(26632): ... 11 more
11-09 13:02:42.887: I/Process(26632): Sending signal. PID: 26632 SIG: 9
11-09 13:02:37.480: D/libEGL(26632): loaded /system/lib/egl/libGLES_android.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
11-09 13:02:37.488: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
11-09 13:02:37.496: D/libEGL(26632): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
11-09 13:02:37.637: D/OpenGLRenderer(26632): Enabling debug mode 0
11-09 13:02:40.582: D/dalvikvm(26632): GC_CONCURRENT freed 189K, 2% free 14334K/14599K, paused 3ms+2ms
11-09 13:02:40.582: W/CursorWrapperInner(26632): Cursor finalized without prior close()
11-09 13:02:40.691: I/Maps.MyLocationOverlay(26632): Request updates from network
11-09 13:02:40.707: D/dalvikvm(26632): GC_CONCURRENT freed 121K, 2% free 14613K/14855K, paused 2ms+2ms
11-09 13:02:40.855: D/AndroidRuntime(26632): Shutting down VM
11-09 13:02:40.855: W/dalvikvm(26632): threadid=1: thread exiting with uncaught exception (group=0x40a3f1f8)
11-09 13:02:40.855: E/AndroidRuntime(26632): FATAL EXCEPTION: main
11-09 13:02:40.855: E/AndroidRuntime(26632): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.os.Looper.loop(Looper.java:137)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-09 13:02:40.855: E/AndroidRuntime(26632): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:02:40.855: E/AndroidRuntime(26632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 13:02:40.855: E/AndroidRuntime(26632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 13:02:40.855: E/AndroidRuntime(26632): at dalvik.system.NativeStart.main(Native Method)
11-09 13:02:40.855: E/AndroidRuntime(26632): Caused by: java.lang.NullPointerException
11-09 13:02:40.855: E/AndroidRuntime(26632): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
11-09 13:02:40.855: E/AndroidRuntime(26632): at org.example.loyaltier.SimpleItemizedOverlay.addOverlay(SimpleItemizedOverlay.java:27)
11-09 13:02:40.855: E/AndroidRuntime(26632): at org.example.loyaltier.Location.onCreate(Location.java:85)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.Activity.performCreate(Activity.java:4465)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-09 13:02:40.855: E/AndroidRuntime(26632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-09 13:02:40.855: E/AndroidRuntime(26632): ... 11 more
11-09 13:02:42.887: I/Process(26632): Sending signal. PID: 26632 SIG: 9
答案 0 :(得分:0)
首先,如果您只是向我们显示错误报告,我们将无法帮助您,您需要显示实际代码。
其次有很多资源可以教你什么是空指针异常,所以你可以调试你的程序,这里是一个:What is a NullPointerException, and how do I fix it?
答案 1 :(得分:0)
OverlayItem overlayItem=new OverlayItem(searchLocation,text_location.getText().toString(),"for test");
text_location.getText()
一开始就会为空,因为你已将它放在keyListener之外。
尝试输入此代码
mapOverlays=map.getOverlays();
drawable=getResources().getDrawable(R.drawable.marker);
itemizedOverlay=new SimpleItemizedOverlay(drawable,map);
OverlayItem overlayItem=new OverlayItem(searchLocation,text_location.getText().toString(),"for test");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
进入if(keyCode==KeyEvent.KEYCODE_ENTER){
区域。