onCreate中的MapView NullPointerException

时间:2013-01-19 00:37:32

标签: android constructor nullpointerexception android-mapview oncreate

我正在以编程方式创建MapView

MapView mv = new MapView(this);

然而,这导致NullPointerException(见日志)。如果我使用其他构造函数,如

MapView mv = new MapView(this, new GoogleMapOptions());
一切都很好。怎么了?

的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listings);

    mSmallMapView = new MapView(this);
    mSmallMapView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 200));

    this.addView(mSmallMapView);

    mSmallMapView.onCreate(savedInstanceState);
}

logcat的:

01-19 01:28:20.411: E/AndroidRuntime(16039): FATAL EXCEPTION: main
01-19 01:28:20.411: E/AndroidRuntime(16039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.ui.ListingsActivity}: java.lang.NullPointerException
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.os.Looper.loop(Looper.java:137)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at java.lang.reflect.Method.invoke(Method.java:511)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at dalvik.system.NativeStart.main(Native Method)
01-19 01:28:20.411: E/AndroidRuntime(16039): Caused by: java.lang.NullPointerException
01-19 01:28:20.411: E/AndroidRuntime(16039):    at maps.am.r.a(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at maps.y.ae.a(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at maps.y.bm.onCreate(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.maps.internal.IMapViewDelegate$Stub.onTransact(IMapViewDelegate.java:66)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.os.Binder.transact(Binder.java:310)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.maps.internal.IMapViewDelegate$a$a.onCreate(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.maps.MapView$b.onCreate(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.internal.d$3.a(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.internal.i.b(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.maps.MapView$a.a(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.maps.MapView$a.a(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.internal.d.a(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.internal.d.onCreate(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.google.android.gms.maps.MapView.onCreate(Unknown Source)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at com.example.ui.ListingsActivity.onCreate(ListingsActivity.java:108)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.Activity.performCreate(Activity.java:5104)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-19 01:28:20.411: E/AndroidRuntime(16039):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-19 01:28:20.411: E/AndroidRuntime(16039):    ... 11 more

1 个答案:

答案 0 :(得分:1)

您可能需要初始化地图才能使其生效。

尝试致电:

try {
    MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}
在'setContentView()'之后的onCreate()方法中