地图不断崩溃我的应用程序

时间:2014-05-15 18:03:46

标签: android maps google-maps-api-2 android-maps

我的菜单工作,但是当我尝试点击地图时,它会因某些原因而崩溃。我检查了看起来不错的API密钥。

main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:name="com.google.android.gms.maps.MapFragment"/>  

可能不相关:链接到查看main.xml页面的代码

    package com.example.DrinkDealzApplication;

    import info.androidhive.slidingmenu.R;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class WhatsHotFragment extends Fragment {

        public WhatsHotFragment(){}


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

            View rootView = inflater.inflate(R.layout.main, container, false);
             //changed from R.layout.fragment_whats_hot to R.layout.main 
            return rootView;
        }
    }

清单:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
<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"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<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.DrinkDealzApplication.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>
        <uses-library android:name="com.google.android.maps"/>
  <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyC7WDlHZ6OMODyug3PZRZeCSAladGJUnvE"/>      
    </application>

</manifest>

logcat

05-15 19:08:03.290: D/AbsListView(31732): Get MotionRecognitionManager
05-15 19:08:03.365: D/libEGL(31732): loaded /system/lib/egl/libEGL_mali.so
05-15 19:08:03.375: D/libEGL(31732): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-15 19:08:03.375: D/libEGL(31732): loaded /system/lib/egl/libGLESv2_mali.so
05-15 19:08:03.380: D/(31732): Device driver API match
05-15 19:08:03.380: D/(31732): Device driver API version: 10
05-15 19:08:03.380: D/(31732): User space API version: 10 
05-15 19:08:03.380: D/(31732): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
05-15 19:08:03.410: D/OpenGLRenderer(31732): Enabling debug mode 0
05-15 19:08:03.455: D/dalvikvm(31732): GC_CONCURRENT freed 127K, 6% free 12469K/13191K, paused 17ms+4ms, total 31ms
05-15 19:08:03.460: D/dalvikvm(31732): WAIT_FOR_CONCURRENT_GC blocked 8ms
05-15 19:08:23.725: D/AndroidRuntime(31732): Shutting down VM
05-15 19:08:23.725: W/dalvikvm(31732): threadid=1: thread exiting with uncaught exception (group=0x4143f2a0)
05-15 19:08:23.735: E/AndroidRuntime(31732): FATAL EXCEPTION: main
05-15 19:08:23.735: E/AndroidRuntime(31732): android.view.InflateException: Binary XML file line #1: Error inflating class fragment
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at com.example.DrinkDealzApplication.CommunityFragment.onCreateView(CommunityFragment.java:18)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.Activity.onCreateView(Activity.java:4876)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at com.example.DrinkDealzApplication.CommunityFragment.onCreateView(CommunityFragment.java:18)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.BackStackRecord.run(BackStackRecord.java:635)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.os.Handler.handleCallback(Handler.java:615)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.os.Looper.loop(Looper.java:137)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at java.lang.reflect.Method.invokeNative(Native Method)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at java.lang.reflect.Method.invoke(Method.java:511)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at dalvik.system.NativeStart.main(Native Method)
05-15 19:08:23.735: E/AndroidRuntime(31732): Caused by: java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f0a0010, tag null, or parent id 0x7f0a0001 with another fragment for com.google.android.gms.maps.SupportMapFragment
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.app.Activity.onCreateView(Activity.java:4862)
05-15 19:08:23.735: E/AndroidRuntime(31732):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-15 19:08:23.735: E/AndroidRuntime(31732):    ... 24 more

1 个答案:

答案 0 :(得分:0)

首先删除导入info.androidhive.slidingmenu.R;

使用SupportMapFragment

所以像这样替换xml中的片段

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

在你的onCreate()中,像这样初始化地图

// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment)
       getSupportFragmentManager().findFragmentById(R.id.map2);
// Getting GoogleMap object from the fragment
 googleMap = fm.getMap();

这是用于显示地图的正确的 Android_manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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" />

<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.example.package.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.gms.version"
    android:value="@integer/google_play_services_version" />
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="**Replace with your key**" />

别忘了添加google-play-services_lib