当我将Fragment用于Google地图时,为什么我的应用程序崩溃了

时间:2014-03-14 04:48:35

标签: android google-maps android-fragments android-maps-v2

我尝试运行谷歌地图应用程序但是当我在我的xml文件中放置地图Fragmet时,应用程序停止。我认为问题在于片段映射,因为当我在没有它的情况下运行应用程序时,例如只添加一个按钮运行完美,我也将清单放回去查看 这是我的java代码:

public class Activity2 extends FragmentActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_activity2,
                container, false);
        return rootView;
    }
}

}

这是我的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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tutorialgooglemaps.Activity2$PlaceholderFragment" >

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

</RelativeLayout>

和myLOGCAT

03-14 00:47:38.619: E/AndroidRuntime(3717): FATAL EXCEPTION: main
03-14 00:47:38.619: E/AndroidRuntime(3717): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorialgooglemaps/com.example.tutorialgooglemaps.Activity2}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.os.Looper.loop(Looper.java:137)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.ActivityThread.main(ActivityThread.java:4517)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at dalvik.system.NativeStart.main(Native Method)
03-14 00:47:38.619: E/AndroidRuntime(3717): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.example.tutorialgooglemaps.Activity2$PlaceholderFragment.onCreateView(Activity2.java:59)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1137)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.Activity.performStart(Activity.java:4480)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1943)
03-14 00:47:38.619: E/AndroidRuntime(3717):     ... 11 more
03-14 00:47:38.619: E/AndroidRuntime(3717): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4242000 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" />
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.maps.SupportMapFragment$b.ex(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.dynamic.a.a(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
03-14 00:47:38.619: E/AndroidRuntime(3717):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
03-14 00:47:38.619: E/AndroidRuntime(3717):     ... 24 more

这是清单

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

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

    <permission
        android:name="org.example.ejemplogooglemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="org.example.ejemplogooglemaps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <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="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.tutorialgooglemaps.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="AIzaSyDrcKDGxUgl0Ps4u2ptOsIUCI6ooXmZ0RU" />

        <activity
            android:name="com.example.tutorialgooglemaps.Activity2"
            android:label="@string/title_activity_activity2" >
        </activity>
    </application>


</manifest>

1 个答案:

答案 0 :(得分:2)

你的logcat清楚地说了

03-14 00:47:38.619: E/AndroidRuntime(3717): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4242000 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" />

将Google Play服务版本添加到您应用的清单

  

编辑应用程序的AndroidManifest.xml文件,然后添加   在元素中声明如下。这嵌入了版本   使用该应用编译的Google Play服务。

您需要将<meta-data>标记下的<application>添加到AndroidManifest.xml

....<application>
<meta-data android:name="com.google.android.gms.version"  
android:value="@integer/google_play_services_version" />
</application>

这是因为最新的Google Play服务需要一个版本名称,使用<meta-data .. />内的AndroidManifest.xml来提及

更新:构建您的manifest.xml,如:

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

<meta-data android:name="com.google.android.gms.version"  
android:value="@integer/google_play_services_version" />

</application>