android编程中遇到的关于 java.lang.NullPointerException错误的原因及解决办法

时间:2021-07-08 15:23:20

LogCat中的错误提示如下:

04-29 09:43:23.390: E/AndroidRuntime(24967): FATAL EXCEPTION: main
04-29 09:43:23.390: E/AndroidRuntime(24967): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.baidu.map/com.baidu.map.RoutePlan}: java.lang.NullPointerException
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1729)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1745)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.os.Looper.loop(Looper.java:130)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.ActivityThread.main(ActivityThread.java:3809)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at java.lang.reflect.Method.invoke(Method.java:507)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at dalvik.system.NativeStart.main(Native Method)
04-29 09:43:23.390: E/AndroidRuntime(24967): Caused by: java.lang.NullPointerException
04-29 09:43:23.390: E/AndroidRuntime(24967):  at com.baidu.map.RoutePlan.onCreate(RoutePlan.java:45)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 09:43:23.390: E/AndroidRuntime(24967):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1693)
04-29 09:43:23.390: E/AndroidRuntime(24967):  ... 11 more

 

错误原因:

04-29 09:43:23.390: E/AndroidRuntime(24967): Caused by: java.lang.NullPointerException
04-29 09:43:23.390: E/AndroidRuntime(24967):  at com.baidu.map.RoutePlan.onCreate(RoutePlan.java:45)

错误出现在RoutePlan.java这个类的第45行,第44行至第45行如下:

mMapView=(MapView)findViewById(R.id.bmapview);
  mMapView.setBuiltInZoomControls(true);

 

错误的原因是找不到对象:这个对象应该是指在第44行初始化的mMapView,应该是初始化错误造成的,仔细检查了一下,初始化时应该写成R.id.bmapView,而我指定的id却

写成了R.id.bmapview,因而初始化没有成功,就造成了没有找到对象的错误。