3

我正在以MapView这样的方式创建一个:

MapView mv = new MapView(this);

然而,这导致NullPointerException(见日志)。如果我使用另一个构造函数,例如

MapView mv = new MapView(this, new GoogleMapOptions());

一切都很好。有什么事?

创建:

@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);
}

日志猫:

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
4

1 回答 1

1

您可能需要初始化 Maps 才能使其正常工作。

尝试调用:

try {
    MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}

在“setContentView()”之后的 onCreate() 方法中。

于 2013-01-30T01:02:16.273 回答