Android谷歌地图API V2 - 错误膨胀类碎片

时间:2014-04-02 22:06:12

标签: android api google-maps android-fragments sdk

我一直在关注Googles指南,制作一款可让您在Android手机上查看应用的应用。我已经遵循了很多教程,但总是会出现同样的错误!我也在网上搜索并发现我的错误与"片段"有关。这是我的文件:

MainActivity.java

package com.example.theapp;

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);
    }
}

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"/>

的AndroidManifest.xml

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

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

<permission
    android:name="com.example.theapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

<uses-permission android:name="com.example.theapp.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"/>
<!-- 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"/>     

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


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.theapp.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" />

</application>

我使用的是Android 2.3。这是log cat消息:

04-02 22:43:45.890: D/AndroidRuntime(23773): Shutting down VM
04-02 22:43:45.890: W/dalvikvm(23773): threadid=1: thread exiting with uncaught exception    (group=0x40018578)
04-02 22:43:45.898: E/AndroidRuntime(23773): FATAL EXCEPTION: main
04-02 22:43:45.898: E/AndroidRuntime(23773): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.theapp/com.example.theapp.MainActivity}:   android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.os.Looper.loop(Looper.java:130)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.reflect.Method.invoke(Method.java:507)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at dalvik.system.NativeStart.main(Native Method)
04-02 22:43:45.898: E/AndroidRuntime(23773): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.Activity.setContentView(Activity.java:1657)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.example.theapp.MainActivity.onCreate(MainActivity.java:11)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-02 22:43:45.898: E/AndroidRuntime(23773):    ... 11 more
04-02 22:43:45.898: E/AndroidRuntime(23773): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.theapp-1.apk]
04-02 22:43:45.898: E/AndroidRuntime(23773):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-02 22:43:45.898: E/AndroidRuntime(23773):    ... 19 more
04-02 22:55:53.289: D/AndroidRuntime(23942): Shutting down VM
04-02 22:55:53.289: W/dalvikvm(23942): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-02 22:55:53.304: E/AndroidRuntime(23942): FATAL EXCEPTION: main
04-02 22:55:53.304: E/AndroidRuntime(23942): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.theapp/com.example.theapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.os.Looper.loop(Looper.java:130)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.reflect.Method.invoke(Method.java:507)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at dalvik.system.NativeStart.main(Native Method)
04-02 22:55:53.304: E/AndroidRuntime(23942): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.Activity.setContentView(Activity.java:1657)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.example.theapp.MainActivity.onCreate(MainActivity.java:11)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-02 22:55:53.304: E/AndroidRuntime(23942):    ... 11 more
04-02 22:55:53.304: E/AndroidRuntime(23942): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.theapp-2.apk]
04-02 22:55:53.304: E/AndroidRuntime(23942):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-02 22:55:53.304: E/AndroidRuntime(23942):    ... 19 more

任何帮助将不胜感激。提前谢谢。

3 个答案:

答案 0 :(得分:1)

     package com.example.theapp;

      import android.app.Activity;
              import android.os.Bundle;

             public class MainActivity extends FragmentActivity {

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

并在清单中添加另一个元标记。

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

答案 1 :(得分:0)

据我记忆,您应该在Android SDK文件夹“sdk / extras / android / support / v4 / android-support-v4.jar”中包含android-support-v4.jar文件。如果您没有该文件,请在您的SDK Manager中安装Android支持库,该支持库位于Extras。

答案 2 :(得分:0)

请检查以下步骤:

  1. 在Android清单中,您拥有此类

    的NETWORK STATE权限

    &LT; uses-permission android:name =“android.permission.ACCESS_NETWORK_STATE”/&gt;

  2. 在Android清单中,请确保您还拥有以下代码 -

      <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value=" YOUR KEY HERE" />
    
     <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />  
    
  3. 最后,如果你在Android API 5.x上测试你的应用程序,那么请确保你修改你的地图代码 -

  4. 而不是这个

     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    

    写这个

    map = getMapFragment().getMap();
    

    并添加一个像这样的小方法

    private MapFragment getMapFragment() {
            FragmentManager fm = null;
    
            Log.d("", "sdk: " + Build.VERSION.SDK_INT);
            Log.d("", "release: " + Build.VERSION.RELEASE);
    
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                Log.d("", "using getFragmentManager");
                fm = getFragmentManager();
            } else {
                Log.d("", "using getChildFragmentManager");
                fm = getChildFragmentManager();
            }
    
            return (MapFragment) fm.findFragmentById(R.id.map);
        }
    

    并确保第3步你的清单文件中的min sdk版本是17这样的

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