android.view.InflateException:二进制XML文件中的行#20:错误充气类片段 [英] android.view.InflateException: Binary XML file line #20: Error inflating class fragment

查看:270
本文介绍了android.view.InflateException:二进制XML文件中的行#20:错误充气类片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了类似主题的计算器,但它并没有帮助我。 我显示谷歌地图使用片段崩溃后获得另一个片段回来。 换句话说,谷歌地图上显示了仅一次和崩溃。 下面是C $ CS的$。

 公共类MapTabMainFragment扩展BaseFragment {
    私人AD浏览报AD浏览报;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        查看查看= NULL;
        尝试 {
            鉴于= inflater.inflate(R.layout.fragment_map_main,集装箱,
                    假);
        }赶上(例外五){
            e.printStackTrace();
        }

        的initComponents(视图);
        initValues​​();
        initListeners();

        返回查看;
    }

    公共无效的initComponents(查看视图){
        AD浏览报=(AD浏览报)view.findViewById(R.id.adView1);
    }

    公共无效initValues​​(){
        AdRequest重=新AdRequest();
        adView.loadAd(重);
    }

    公共无效initListeners(){

    }
}

 公共类BaseFragment扩展片段{

    公共BottomTabActivity mActivity;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

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

    公共布尔onBack pressed(){
        返回false;
    }

    公共无效onActivityResult(INT申请code,INT结果code,意图数据){

    }
}
 

当我试图捕获异常,这是

  

android.view.InflateException:二进制XML文件中的行#20:错误充气类片段

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/lib/com.google.ads
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@色/ WhiteColor
    机器人:方向=垂直>

    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/头/>

    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_marginTop =@扪/ data_listview_margin_top>

        <片段
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID /图
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_marginBottom =@扪/ data_listview_margin_bottom
            类=com.google.android.gms.maps.SupportMapFragment/>
        <! - 类=com.cyrilmottier.polaris2.maps.SupportMapFragment - >

        < com.google.ads.AdView
            机器人:ID =@ + ID / adView1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentBottom =真
            应用程序:adSize =SMART_BANNER
            应用程序:adUnitId =CA-APP-酒馆3761995838分之9766031373541061
            应用程序:loadAdOnCreate =真
            应用程序:testDevices =TEST_EMULATOR,TEST_DEVICE_ID>
        < /com.google.ads.AdView>
    < / RelativeLayout的>

< / LinearLayout中>
 

解决方案

您已经不能嵌套在XML片段。你应该这样做在code。 最好的方法是创建一个的FrameLayout,并与您的片段在运行时进行更换。

假设XML是如下:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:广告=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:ID =@ + ID / inquiryLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=InquiryFragment。>

<的FrameLayout
    机器人:ID =@ + ID / contentFrame
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentLeft =真正的>

< /的FrameLayout>
 < / RelativeLayout的>
 

现在的code是简单的:

 公共类InquiryFragment扩展片段{

    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        //充气的布局该片段
        视图V = inflater.inflate(R.layout.inquiry_fragment,集装箱,假);

        Plan1Fragment FRAG =新Plan1Fragment();
        getChildFragmentManager()的BeginTransaction()
            .replace(R.id.contentFrame,FRAG).commit();

        返回伏;
    }

}
 

这就是它。

I found similar topics in stackoverflow but it didn't help for me. I am showing google map using fragment and it crashes after get another fragment and come back. In other words, google map shows only once and crashes. Here are the codes.

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) {

    }
}

when I try to catch the exception, it is

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>

解决方案

You cannot have nested fragments in XML. You should do it in code. The best way is to create a FrameLayout, and replace it with your fragment at run-time.

Suppose the XML is as follows:

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

Now the code is simple:

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

}

And that's it.

这篇关于android.view.InflateException:二进制XML文件中的行#20:错误充气类片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆