当前位置: 首页 > 知识库问答 >
问题:

Android.view.inflateexception:二进制XML文件第7行:二进制XML文件第7行:inflating类片段时出错

徐绪
2023-03-14

我刚刚实现了我的第四个选项卡到我的应用程序中,已经实现了谷歌地图。在此实现之前,我的应用程序运行良好,但现在当我更改选项卡时,它会崩溃。我在这里看到过类似的问题,但没有一个有答案(实际上有这么多不同的原因和回应这个问题)

下面是fragmentshouts_maps类

package com.example.hp_user.shoutfinal28;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
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;


public class FragmentShouts_Maps extends Fragment implements OnMapReadyCallback {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment shouts.xml
    View root_view = inflater.inflate(R.layout.fragmentshouts_maps, container, false);
    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Maps1);
    if (supportMapFragment!= null) {
        supportMapFragment.getMapAsync(this);
    }
    return root_view;
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onMapReady (GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}

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

<fragment
    android:id="@+id/Maps1"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/fragmentshouts_maps"
    android:layout_alignParentEnd="true">
</fragment>


</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="SHOUT"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="SHOUT"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


</manifest>

查看寻呼机适配器类

package com.example.hp_user.shoutfinal28;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;



public class ViewPagerAdapter extends FragmentStatePagerAdapter {

final int PAGE_COUNT = 4;
// Tab Titles
private String tabtitles[] = new String[] {"Home","Shouts","Maps","Shouters"};
Context context;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public int getCount() {
    return PAGE_COUNT;
}


public Fragment getItem(int position) {
    switch (position) {

        // Open Fragment home.java
        case 0:
            FragmentHome fragmenthome = new FragmentHome();
            return fragmenthome;

        // Open Fragment shouters.java
        case 1:
            FragmentShouts fragmentshouts = new FragmentShouts();
            return fragmentshouts;

        case 2:
            FragmentShouts_Maps fragmentshouts_maps = new FragmentShouts_Maps();
            return fragmentshouts_maps;

        case 3:
            FragmentShouters fragmentshouters = new FragmentShouters();
            return fragmentshouters;



    }
    return null;
}

@Override
public CharSequence getPageTitle(int position) {
    return tabtitles[position];
}


}

这是我的错误日志

共有1个答案

向泽语
2023-03-14

尝试在OnActivityCreated()执行相同的操作

   public void onActivityCreated(Bundle savedInstanceState) {
                    super.onActivityCreated(savedInstanceState);
                    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Maps1);
                    if (supportMapFragment!= null) {
                        supportMapFragment.getMapAsync(this);
                    }
                }
 类似资料: