0

我一直在尝试创建一个我想象的相当简单的应用程序——在开始活动上,有一个按钮可以按下,然后会启动一个显示您当前位置的 MapActivity。

但是,每当我按下按钮时,我都会立即收到错误消息并被迫退出应用程序。

这是日志:

10-23 19:15:10.826: D/PhoneWindow(3227): DebugMonitor class=com.android.find.my.friends.MainActivity focus=true
10-23 19:15:19.496: D/AndroidRuntime(3227): Shutting down VM
10-23 19:15:19.496: W/dalvikvm(3227): threadid=3: thread exiting with uncaught exception (group=0x4001db88)
10-23 19:15:19.496: E/AndroidRuntime(3227): Uncaught handler: thread main exiting due to uncaught exception
10-23 19:15:19.506: E/AndroidRuntime(3227): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.find.my.friends/com.android.find.my.friends.ShowMap}: java.lang.NullPointerException
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2486)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.ActivityThread.access$2100(ActivityThread.java:123)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1843)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.os.Looper.loop(Looper.java:123)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.ActivityThread.main(ActivityThread.java:4321)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at java.lang.reflect.Method.invoke(Method.java:521)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at dalvik.system.NativeStart.main(Native Method)
10-23 19:15:19.506: E/AndroidRuntime(3227): Caused by: java.lang.NullPointerException
10-23 19:15:19.506: E/AndroidRuntime(3227):     at com.android.find.my.friends.ShowMap.onCreate(ShowMap.java:30)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
10-23 19:15:19.506: E/AndroidRuntime(3227):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2418)
10-23 19:15:19.506: E/AndroidRuntime(3227):     ... 11 more
10-23 19:15:19.516: I/dalvikvm(3227): threadid=7: reacting to signal 3
10-23 19:15:19.546: I/dalvikvm(3227): Wrote stack trace to '/data/anr/traces.txt'

我正在运行 Android 1.6 的 HTC 纹身上对此进行测试。我以前也能够创建一个只显示 MapActivity 的应用程序。

有帮助吗?

[编辑]

这是我的 onCreate 方法:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // bind the layout to the activity

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setStreetView(true);
    mapController = mapView.getController();
    mapController.setZoom(14); // Zoom 1 is world view
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new GeoUpdateHandler());

}
4

1 回答 1

1

看来您的班级NullPointerException第 30 行有一个。ShowMap如果您可以发布您的onCreate方法和第 30 行,我可以帮助您找出原因。

您能否也粘贴异常本身发生的行?有可能findViewById返回 null,在这种情况下您mapView将是 null。确保定义 MapView 的布局与传入的布局相同setContentView

于 2011-10-23T19:04:18.623 回答