java - android mapView 未解决

标签 java android eclipse google-maps-api-3

我在 eclipse/java 中使用 mapView 时遇到以下问题。

使用mapView.getOverlays()我收到错误。

mapView cannot be resolved

我尝试对 mapView 使用局部变量(使用 MapView),但它崩溃了。 mapView 是否在某处声明?

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

但我无法访问代码和 the offcial doc没有帮助

MapView cannot be resolved or is not a field也没有帮助

我已经安装了 SDK 并在 Eclipse 中检查了 Google API

project build target

我可以毫无问题地显示 map

我的 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 */

最佳答案

将此添加到您的菜单中

<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);

关于java - android mapView 未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12404096/

相关文章:

java - 如何更改 XWPFDocument 中的文本方向?

Java JSF spintax 包含循环引用

android - Horizo​​ntalScrollView 在 RTL 模式下不向左滚动

java.security.NoSuchAlgorithmException : Cannot find any provider supporting Blowfish/ECB/PKCS5Padding

java - 检查是否在android中启用了GPS

java - hibernate框架会改变表结构吗?

java - 我需要了解 Haskell 如何表示数据才能编写好的 Haskell 程序吗?

Android MenuItem 的 showAsAction 获取方法

android - app:compileReleaseJavaWithJavac java.lang.VerifyError

eclipse - 从 Eclipse 中的现有 Java 项目中删除 SVN