在Google Map中输入类片段时出错

时间:2013-12-01 08:13:54

标签: android android-fragments

我刚刚学习Android,在运行Google Map代码时遇到了一些问题。 我在堆栈溢出中尝试了很多解决方案:

  1. Mainactivity.java:扩展FragmentActivity而不是Activity
  2. activity_main.xml:将片段名称更改为“com.samplefragsapplication.SuggestionFrags”或“com.google.android.gms.maps.SupportMapFragment”
  3. 它仍然不起作用。

    这是activity_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"/>
    

    这是Mainactivity.java

    package com.example.asg2b;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    

    这是Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.asg2b"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
    
        <permission
            android:name="com.example.asg2b.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" >
        </permission>
    
        <uses-permission android:name="com.example.asg2b.permission.MAPS_RECEIVE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <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.asg2b.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="Android Key" />
        </application>
    
    </manifest>
    

    这是我从LogCat获得的日志

    12-01 14:48:08.705: D/dalvikvm(14191): GC_CONCURRENT freed 312K, 4% free 12919K/13447K, paused 4ms+3ms
    12-01 14:48:08.870: D/dalvikvm(14191): GC_CONCURRENT freed 306K, 5% free 13073K/13639K, paused 1ms+7ms
    12-01 14:48:09.055: D/dalvikvm(14191): GC_CONCURRENT freed 360K, 5% free 13179K/13767K, paused 3ms+6ms
    12-01 14:48:09.090: D/AndroidRuntime(14191): Shutting down VM
    12-01 14:48:09.090: W/dalvikvm(14191): threadid=1: thread exiting with uncaught exception (group=0x40c681f8)
    12-01 14:48:09.110: E/AndroidRuntime(14191): FATAL EXCEPTION: main
    12-01 14:48:09.110: E/AndroidRuntime(14191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asg2b/com.example.asg2b.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.os.Handler.dispatchMessage(Handler.java:99)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.os.Looper.loop(Looper.java:137)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.ActivityThread.main(ActivityThread.java:4514)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at dalvik.system.NativeStart.main(Native Method)
    12-01 14:48:09.110: E/AndroidRuntime(14191): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.Activity.setContentView(Activity.java:1892)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.example.asg2b.MainActivity.onCreate(MainActivity.java:11)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.Activity.performCreate(Activity.java:4562)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    ... 11 more
    12-01 14:48:09.110: E/AndroidRuntime(14191): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 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" />
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.dynamic.a.a(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.app.Activity.onCreateView(Activity.java:4339)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
    12-01 14:48:09.110: E/AndroidRuntime(14191):    ... 21 more
    12-01 14:48:18.250: D/dalvikvm(14191): GC_CONCURRENT freed 516K, 6% free 13130K/13895K, paused 5ms+4ms
    

1 个答案:

答案 0 :(得分:0)

将此信息放在应用程序代码

下的清单中
  <meta-data
            android:name="com.google.android.gms.version"
            android:value="4030500" />

编辑:

并用此代码替换您的activity_main.xml代码

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
         />