2012-09-13 64 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);

愚蠢的错误。