膨胀类片段时出错(android.view.InflateExpection)

时间:2016-01-27 16:22:43

标签: java android xml google-maps android-fragments

所以我试图将Google地图合并到一个片段中,我已经按照Maps上的Google Dev文档中提到的步骤进行了操作,但是我在填充类片段时出错了。我经历了很多堆栈并完成了其中提到的所有内容,但错误仍然存​​在。这是我的Map Fragment,

Fragment_two.java

    package com.example.TheWeddingApp;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment implements OnMapReadyCallback {

    MapView mapView;
    GoogleMap map;

    public static Fragment newInstance(Context context) {
        FragmentTwo f = new FragmentTwo();
        return f;
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_two, null);
        getActivity().setContentView(R.layout.fragment_two);
        MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        return root;
    }
    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
    }


}

我的地图xml,

fragment_two.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"
    android:orientation="vertical" >
<fragment android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

这是我的清单

的AndroidManifest.xml

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

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

    <uses-permission android:name="com.example.TheWeddingApp.permission.MAPS_RECEIVE" />


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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <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" >
            <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="AIzaSyBTbyjkzYDeEFGsljSoLBr54riAjhWCVJg" />
        <activity
            android:name="com.example.TheWeddingApp.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>


    </application>

</manifest>

这是logcat

        01-27 21:43:09.811: E/AndroidRuntime(15412): FATAL EXCEPTION: main
    01-27 21:43:09.811: E/AndroidRuntime(15412): Process: com.example.TheWeddingApp, PID: 15412
    01-27 21:43:09.811: E/AndroidRuntime(15412): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.app.Activity.setContentView(Activity.java:1937)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at com.example.TheWeddingApp.FragmentTwo.onCreateView(FragmentTwo.java:35)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.os.Handler.handleCallback(Handler.java:733)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.os.Handler.dispatchMessage(Handler.java:95)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.os.Looper.loop(Looper.java:136)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.app.ActivityThread.main(ActivityThread.java:5086)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at java.lang.reflect.Method.invoke(Method.java:515)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at dalvik.system.NativeStart.main(Native Method)
    01-27 21:43:09.811: E/AndroidRuntime(15412): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f080035, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    01-27 21:43:09.811: E/AndroidRuntime(15412):    ... 22 more

更新#1

实现了对此帖子的回复,但在调用地图时仍然出现空指针错误。不知怎的,我无法掌握地图。这是我得到错误的地方 mapFragment.getMapAsync(this); 这是更新后的logcat:

    01-27 23:04:50.140: E/AndroidRuntime(21288): FATAL EXCEPTION: main
01-27 23:04:50.140: E/AndroidRuntime(21288): Process: com.example.TheWeddingApp, PID: 21288
01-27 23:04:50.140: E/AndroidRuntime(21288): java.lang.NullPointerException
01-27 23:04:50.140: E/AndroidRuntime(21288):    at com.example.TheWeddingApp.FragmentTwo.onCreateView(FragmentTwo.java:36)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.os.Handler.handleCallback(Handler.java:733)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.os.Looper.loop(Looper.java:136)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at android.app.ActivityThread.main(ActivityThread.java:5086)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at java.lang.reflect.Method.invoke(Method.java:515)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-27 23:04:50.140: E/AndroidRuntime(21288):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案