【问题标题】:android mapView not resolvedandroid mapView 未解决
【发布时间】:2012-09-06 09:54:57
【问题描述】:

我在 eclipse/java 中的 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 中检查了 Google 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 */

【问题讨论】:

  • 您安装了 Google API 吗??
  • 你在代码中声明了 MapView 吗?
  • 尝试将“extends Activity”改为“extends MapActivity”

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


【解决方案1】:

将此添加到您的清单中

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

【讨论】:

  • 是的,但是当我编辑我的答案时,你也发现了你的愚蠢错误。 :)
【解决方案2】:

好的,问题解决了。

我必须使用setContentView(R.layout.activity_main);

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

愚蠢的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2016-05-22
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2010-12-22
    • 2018-03-06
    相关资源
    最近更新 更多