微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android.view.InflateException:二进制XML文件行#20:错误膨胀类片段

我在stackoverflow中发现了类似的主题,但它对我没有帮助.
我正在使用片段显示谷歌地图,它在获得另一个片段后崩溃并返回.
换句话说,谷歌地图只显示一次并崩溃.
这是代码.

public class MapTabMainFragment extends BaseFragment {
    private AdView adView;

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

        View view = null;
        try {
            view = inflater.inflate(R.layout.fragment_map_main, container,
                    false);
        } catch (Exception e) {
            e.printstacktrace();
        }

        initComponents(view);
        initValues();
        initListeners();

        return view;
    }

    public void initComponents(View view) {
        adView = (AdView) view.findViewById(R.id.adView1);
    }

    public void initValues() {
        AdRequest re = new AdRequest();
        adView.loadAd(re);
    }

    public void initListeners() {

    }
}

 public class BaseFragment extends Fragment {

    public Bottomtabactivity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = (Bottomtabactivity) this.getActivity();
    }

    public boolean onBackpressed() {
        return false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }
}

当我试图捕捉异常时,它是

android.view.InflateException: Binary XML file line #20: Error inflating class fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/WhiteColor"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/header" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/data_listview_margin_top" >

        <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:layout_marginBottom="@dimen/data_listview_margin_bottom"
            class="com.google.android.gms.maps.SupportMapFragment" />
        <!--class="com.cyrilmottier.polaris2.maps.SupportMapFragment"  -->

        <com.google.ads.AdView
            android:id="@+id/adView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:adSize="SMART_BANNER"
            app:adUnitId="ca-app-pub-9766031373541061/3761995838"
            app:loadAdOnCreate="true"
            app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
        </com.google.ads.AdView>
    </RelativeLayout>

</LinearLayout>

解决方法:

您不能在XML中嵌套片段.你应该在代码中做到这一点.
最好的方法是创建一个FrameLayout,并在运行时将其替换为您的片段.

假设XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/inquiryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InquiryFragment" >

<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >

</FrameLayout>
 </RelativeLayout>

现在代码很简单:

public class InquiryFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.inquiry_fragment, container, false);

        Plan1Fragment frag = new Plan1Fragment();
        getChildFragmentManager().beginTransaction()
            .replace(R.id.contentFrame, frag).commit();

        return v;
    }

}

就是这样.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐