使用ViewPager时出现Android View InflateException

使用ViewPager时出现Android View InflateException,android,android-viewpager,layout-inflater,supportmapfragment,Android,Android Viewpager,Layout Inflater,Supportmapfragment,我正在尝试在ViewPager中使用MapFragment。尽管我将FragmentManager定义为SupportFragmentManager,但如果我滑到包含MapFragment的页面,我会看到InflateException。我读了这个问题提到的大多数标题,但还看不到解决方案。我希望你能。以下是我的MapFragment类的布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and

我正在尝试在ViewPager中使用MapFragment。尽管我将FragmentManager定义为SupportFragmentManager,但如果我滑到包含MapFragment的页面,我会看到InflateException。我读了这个问题提到的大多数标题,但还看不到解决方案。我希望你能。以下是我的MapFragment类的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="7dp"
        android:orientation="vertical"
        android:gravity="left">

        <ImageButton
            android:id="@+id/searchButton"
            android:background="@drawable/search_button"
            android:layout_marginTop="10dp"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"/>

        <ImageButton
            android:id="@+id/originButton"
            android:background="@drawable/origin_button"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="7dp"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentBottom="true"/>

        <ImageButton
            android:id="@+id/earthButton"
            android:background="@drawable/earth_button"
            android:layout_marginBottom="10dp"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/originButton"/>


    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginRight="14dp"
        android:orientation="vertical"
        android:gravity="right" >

        <ImageButton
            android:id="@+id/plusButton"
            android:background="@drawable/plus_button"
            android:layout_centerInParent="true"
            android:layout_width="50dip"
            android:layout_height="50dip"/>

        <View
            android:layout_width="0dp"
            android:layout_height="7dp"
            android:layout_weight="1"/>

        <ImageButton
            android:id="@+id/minusButton"
            android:background="@drawable/minus_button"
            android:layout_centerInParent="true"
            android:layout_width="50dip"
            android:layout_height="50dip"/>

    </LinearLayout>
</RelativeLayout>
我在ViewPager类的以下部分中遇到错误:

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    viewPager.setCurrentItem(tab.getPosition());
}
最后,build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
    defaultConfig {
        applicationId 'net.geotune.app'
        minSdkVersion 19
        targetSdkVersion 19
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.spotify.sdk:spotify-player:1.0.0-beta9@aar'
    compile 'com.spotify.sdk:spotify-auth:1.0.0-beta9@aar'
    compile files('libs/Android/android-async-http-1.3.1.jar')
    compile files('libs/Android/jackson-core-asl-1.8.5.jar')
    compile files('libs/Android/jackson-mapper-asl-1.8.5.jar')
    compile files('libs/Android/KumulosAndroid.0.2.3.jar')
    compile files('libs/spotify-web-api-android-0.1.0.jar')
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.2.0'
    compile group: 'io.orchestrate', name: 'orchestrate-client', version: '0.9.0'
    compile 'uk.co.chrisjenx:calligraphy:2.0.2'
    compile 'com.sothree.slidinguppanel:library:3.0.0'
}

我知道我在上面添加了太多的代码,对此我很抱歉。我只想千方百计。

渐变和布局文件都没有问题。MapFragment的onCreateView应设置如下:

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

    if (container == null)
        return null;

    if (v != null) {
        ViewGroup parent = (ViewGroup) v.getParent();
        if (parent != null)
            parent.removeView(v);
    }

    try {
        v = inflater.inflate(R.layout.fragment_map, container, false);
    } catch (InflateException e) { }

    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

    if (mapFragment != null) {
        mapFragment.onCreate(savedInstanceState);
        mapFragment.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mapFragment.getMap();
        // Do whatever you want on map here
    }
    return v;    
}

这一问题已在本报告中详细讨论过。因此,我没有详细解释代码段。

Dorukhan,你有幸修复这个家伙吗?耶。我将为你们补充这个问题的答案。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if (container == null)
        return null;

    if (v != null) {
        ViewGroup parent = (ViewGroup) v.getParent();
        if (parent != null)
            parent.removeView(v);
    }

    try {
        v = inflater.inflate(R.layout.fragment_map, container, false);
    } catch (InflateException e) { }

    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

    if (mapFragment != null) {
        mapFragment.onCreate(savedInstanceState);
        mapFragment.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mapFragment.getMap();
        // Do whatever you want on map here
    }
    return v;    
}