2013-03-14 89 views
0

注意:我不想使用MapFragment,因爲我需要覆蓋MapView中的一個方法!Android Google Maps V2,MapView導致NullPointerException

我用下面的代碼來顯示我的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。

我在做什麼錯?

+0

如果您使用Android Google Maps V2,則必須使用SupportMapFragment或MapFragment。 – Jitendra 2013-03-14 13:33:36

+0

它說[這裏](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView)可以直接使用MapView。我必須,因爲我需要重寫某些方法。但這不是問題,因爲我知道它通過xml佈局使用MapView時有效。 – 2013-03-14 15:14:30

回答

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設備,它是最好的。

+0

因爲我想分發我正在寫給第三方開發人員的庫,但不想與他們分享我的源代碼。所以據我所知,使用Android庫項目不是解決方案。我的想法是有一個編譯代碼的jar。 – 2013-03-14 15:16:56

相關問題