2012-09-13 42 views
0

我在eclipse/java中的mapView有以下問題。android mapview未解決

使用mapView.getOverlays()我得到錯誤。

的MapView不能得到解決

我試圖使用MapView類(使用MapView類)的局部變量,但它崩潰。 mapView聲明在某處嗎?

我導入com.google.android.maps.MapView;

,但我不能訪問的代碼和the offcial doc沒有幫助

MapView cannot be resolved or is not a field沒有幫助過

我已經安裝了SDK,並在Eclipse

檢查谷歌API

項目建設目標

我可以毫無問題

顯示地圖和我的MainActivity擴展MapActivity

下面是完整的代碼

package com.example.test_app; 

import java.util.List; 

import android.app.Activity; 
import com.example.test_app.MyLocation; 
import com.example.test_app.MyLocation.LocationResult; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

import android.content.Context; 

import android.location.Location; 
import android.location.LocationProvider; 

import android.location.LocationListener; 

import android.location.LocationManager; 

import android.os.Bundle; 

import android.widget.Toast; 

public class MainActivity extends MapActivity 

{ 


/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) 

{ 

    super.onCreate(savedInstanceState); 
    MapView mapView = (MapView) findViewById(R.id.mapview); 
    if (mapView == null) 
     Toast.makeText(getApplicationContext(),"mapview is null",Toast.LENGTH_SHORT).show(); 
    else 
     mapView.setBuiltInZoomControls(true); 

    setContentView(R.layout.activity_main); 

    LocationResult locationResult = new LocationResult(){ 
     @Override 
     public void gotLocation(Location loc){ 
      Toast.makeText(getApplicationContext()," long : " +loc.getLongitude()+ " lat :"+ loc.getLatitude(),Toast.LENGTH_SHORT).show(); 
      //List<Overlay> mapOverlays = mapView.getOverlays(); 
     } 
    }; 
    MyLocation myLocation = new MyLocation(); 
    myLocation.getLocation(this, locationResult); 

} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

}/* End of UseGps Activity */ 
+0

您是否安裝了Google API? – iNan

+0

你在你的代碼中聲明瞭MapView嗎? –

+0

嘗試將「擴展活動」更改爲「擴展MapActivity」 –

回答

3

在你menifest

<uses-library android:name="com.google.android.maps"/> 
在佈局

添加此

<com.google.android.maps.MapView 
    android:id="@+id/mv" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:apiKey="---- Your Key -------" 
    /> 

你有一個狡猾的錯誤。將您的setContentView移動到MapView的聲明之上。出於這個原因,它得出的錯誤

super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 

     MapView mapView = (MapView) findViewById(R.id.mv); 
+0

已完成=「我可以顯示沒有問題的地圖」 – maazza

+0

我已找到您的問題,我編輯了我的答案。 –

+0

已經完成 – maazza

1

好了,問題解決了。我不得不使用setContentView(R.layout.activity_main);

之前MapView mapView = (MapView) findViewById(R.id.mapview);

愚蠢的錯誤。