Android MapView显示为空

表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yu.lbs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name="com.yu.lbs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:apiKey=".........."
        android:clickable="true"
        android:enabled="true" />

</LinearLayout>

MainActivity.java:

import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.MapActivity;

public class MainActivity extends MapActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

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

}

活动可以开始.但是在MapView中什么都没有.可以显示小网格的地图,我认为它带有MapView,但没有加载地图.可能有什么不对?我使用的是Google Api v3.但是这段代码来自使用API​​ v1的教科书.

解决方法

我得到解决的方式,我不得不这样做:
final MapView mapView = (MapView)fragmentView.findViewById(R.id.map_fieldLocation);

mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng coordinates = new LatLng(match.match.LocationLatitude,match.match.LocationLongitude);
        googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates,15));
        mapView.onResume();
    }
});

我缺少的重要部分是你必须在调用getMapAsync()之前调用onCreate()方法,一旦调用了回调,你需要在MapView对象上调用onResume().

这完全解决了我.

这是你自己班里的样子:

public class MainActivity extends MapActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (getView() != null) {

            final MapView mapView = (MapView)getView().findViewById(R.id.mapView);

            mapView.onCreate(savedInstanceState);
            mapView.getMapAsync(new OnMapReadyCallback() {

                @Override
                public void onMapReady(GoogleMap googleMap) {
                    LatLng coordinates = new LatLng(match.match.LocationLatitude,match.match.LocationLongitude);
                    googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates,15));
                    mapView.onResume();
                }
            }
        }
    }

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

}

希望这可以帮助!

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...