0

我试图让谷歌地图在操作栏选项卡中工作,但我只得到一个空白网格(不是完全空白),没有瓷砖。没有与此选项卡相关的 logcat 错误(映射键有效)。

tab3.java:

    package codekappa.smartlocation;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar;

public class FragmentsTab3 extends Fragment implements ActionBar.TabListener {
    private GoogleMap map;
    private MapView mapView;
    private Fragment mFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment3, container, false);

        // Gets the MapView from the XML layout and creates it
        mapView = (MapView) v.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);

        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
        try {
            MapsInitializer.initialize(this.getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }

        return v;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setContentView(R.layout.fragment3);
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        mFragment = new FragmentsTab3();
        // Attach fragment3.xml layout
        ft.add(android.R.id.content, mFragment);
        ft.attach(mFragment);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        // Remove fragment3.xml layout
        ft.remove(mFragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

片段3.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:name="com.google.android.gms.maps.MapFragment">

    <com.google.android.gms.maps.MapView android:id="@+id/map"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" />

</LinearLayout>

清单.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="codekappa.smartlocation"
    android:versionCode="1"
    android:versionName="1.0" >
<permission
        android:name="codekappa.smartlocation.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="codekappa.smartlocation.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />

    <application


        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.NoActionBar" >

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDLX4a6pyeGsDocAHhjGOKSyeAE0aunHbA"/>

        <activity
            android:name="codekappa.smartlocation.AndroidTabLayoutActivity"
            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>

不知道这里有什么问题,想法?

4

2 回答 2

2

我遇到过类似的问题,我通过添加 onResume() 解决了我的问题。

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}
于 2014-01-15T07:21:41.487 回答
0

您缺少另一个元数据元素。请在带有 Google Map V2 和 SupportMapFragment 的空白地图中查看我的答案(不,这不是 API 密钥问题)

于 2014-01-15T07:49:36.867 回答