Android谷歌地图V2,MapView导致NullPointerException

时间:2013-03-14 13:24:07

标签: android google-maps google-maps-android-api-2

注意:我不想使用MapFragment,因为我需要覆盖MapView内的方法!

我使用以下代码显示我的MapView:

mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(null);
mapView.onResume();

使用这个xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    ...

</RelativeLayout>

这很有效。但我必须将我的项目导出为jar(封闭源代码),所以我不能再使用xml布局了,对吗?

因为我的布局非常简单,所以我可以在代码中手动创建它。 所以现在我正在使用它:

mapView = new MapView(context);
mapView.setLayoutParams(
    new LayoutParams(
        LayoutParams.MATCH_PARENT, 
        LayoutParams.MATCH_PARENT
    )
);
view.addView(mapView);
mapView.onCreate(null);

但是我在onCreate语句中得到了NullPointerException。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GeoPosActivity"
android:orientation="vertical" >

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLocationText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"       
    android:orientation="vertical"
    android:text="TEST"
    android:layout_weight="50"
    />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myMapView"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:enabled="true"
    android:clickable="true"
    android:layout_weight="50"/>

</LinearLayout>

我用它是我的应用程序。它有效,

为什么要使用“.jar”?为了测试您的应用程序使用Android设备是最好的。

相关问题