Android Fragments 如何在Android应用程序中显示谷歌Map

eoigrqb6  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(192)

我想在我的应用程序中实现谷歌Map.我得到了MapAPI密钥,并在我的XML文件中使用它,但谷歌Map不显示。当我添加片段到XML文件,它会导致强制关闭错误,当我纠正这个错误,然后谷歌Map在运行时不显示。
MainActivity.java

package com.example.simplemap;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

字符串
AndroidMainifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <permission
 android:name="com.example.simplemap.permission.MAPS_RECEIVE"
 android:protectionLevel="signature"/>
<uses-permission android:name="com.example.simplemap.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-feature
 android:glEsVersion="0x00020000"
 android:required="true"/> 

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.simplemap.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>
        <meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzaSyBVa_dSU5jYR0A0sUFBDgkYIdmD_PO8ADE"/>
    </application>

</manifest>


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>


logcat:

06-16 15:40:09.827: E/Trace(1343): error opening trace file: No such file or directory (2)
06-16 15:40:10.537: D/AndroidRuntime(1343): Shutting down VM
06-16 15:40:10.537: W/dalvikvm(1343): threadid=1: thread exiting with uncaught exception  (group=0x40a71930)
06-16 15:40:10.557: E/AndroidRuntime(1343): FATAL EXCEPTION: main
06-16 15:40:10.557: E/AndroidRuntime(1343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplemap/com.example.simplemap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.os.Looper.loop(Looper.java:137)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at java.lang.reflect.Method.invoke(Method.java:511)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at dalvik.system.NativeStart.main(Native Method)
06-16 15:40:10.557: E/AndroidRuntime(1343): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.Activity.setContentView(Activity.java:1881)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.example.simplemap.MainActivity.onCreate(MainActivity.java:12)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.Activity.performCreate(Activity.java:5104)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-16 15:40:10.557: E/AndroidRuntime(1343):     ... 11 more
06-16 15:40:10.557: E/AndroidRuntime(1343): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4323000 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.maps.internal.q.B(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.maps.internal.q.A(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.maps.MapFragment$b.gW(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.dynamic.a.a(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.app.Activity.onCreateView(Activity.java:4716)
06-16 15:40:10.557: E/AndroidRuntime(1343):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-16 15:40:10.557: E/AndroidRuntime(1343):     ... 21 more
06-16 15:40:31.647: I/Process(1343): Sending signal. PID: 1343 SIG: 9

insrf1ej

insrf1ej1#

使用以下代码:
片段代码:

public static class MapsFragment extends Fragment {

        private SupportMapFragment suppfrag;
        static final LatLng myLoc = new LatLng(//latitiude and longitude here);
        private GoogleMap map;

        public static final String ARG_SECTION_NUMBER = "section_number";

        public MapsFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                return inflater.inflate(R.layout.fragment_map, container, false);
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            FragmentManager fm = getChildFragmentManager();
            suppfrag = (SupportMapFragment) fm.findFragmentById(R.id.map);
            if (suppfrag == null) {
                suppfrag = SupportMapFragment.newInstance();
                fm.beginTransaction().replace(R.id.map, suppfrag).commit();

                Marker location = map.addMarker(new MarkerOptions().position(CENTRU)
                        .title("Centru"));
                    location.setIcon(BitmapDescriptorFactory
                                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

                    // Move the camera instantly to Bottlecapp with a zoom of 15.
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTRU, 15));

                    // Zoom in, animating the camera.
                    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
                    getActivity().getSupportFragmentManager().popBackStack();
            }
        }
        @Override
        public void onResume() {
            super.onResume();
            if (map == null) {
                map = suppfrag.getMap();
                map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
            }
        }
    }

字符串
xml代码:

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

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

相关问题