我使用Eclipse,Android SDK中的實例被創建和我有一個類,不要把在主要活動中每個代碼:錯誤:MapViews僅能在MapActivity
blablabla
import blablabla
..
..
public class Mhelper {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
Context mContext;
public Mhelper(Context ctx) {
this.mContext= ctx;
}
public void pinpointlocation(Context context){
map = ((SupportMapFragment) ((FragmentActivity) mContext).getSupportFragmentManager().findFragmentById(R.id.mapview))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
....
....
....
,我把一個視圖地圖中的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
...
...
...
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="KEY"
/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button2" />
</RelativeLayout>
而且程序編譯和安裝沒有任何錯誤,但是當我在模擬器中運行應用程序,我得到:
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hach/com.example.hach.MainActivity}: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
第一個只是承認他不應該使用視圖,第二個告訴看這refrence這是好的,但我讀了它,找不到任何解決方案,我可以應用在我的項目。第三個說谷歌地圖Android API v2是一個不錯的選擇。
而我發現this鏈接這是很好的例子,但我不想使用標籤,我應該嗎?除了線
import com.google.android.maps.MapActivity;
是,當我創建新類,我想延長MapActivity的問題,IDE不知道MapActivity。
是否有任何事情可以對我當前的項目進行,而不需要完全更改應用程序,以顯示地圖並指定位置作爲我的代碼?我應該擴展MapActivity並將其添加到項目中嗎?是否有像mapview這樣的簡單解決方案?
好像錯誤說你不能那樣做。爲什麼不只是在你的活動中創建地圖對象,然後把它傳遞給你的幫助類 – tyczj
不使用'com.google.android.maps.MapActivity'這是來自v1 – tyczj
已棄用的地圖活動是的,我這樣做,但同樣的錯誤但這次是從主要活動。 –