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

标签 android xml google-maps fragment

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

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

    }
}

当我 try catch 异常时,它是

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 中不能嵌套 fragment 。你应该用代码来做。 最好的方法是创建一个 FrameLayout,并在运行时将其替换为您的 fragment 。

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

}

就是这样。

关于android.view.InflateException : Binary XML file line #20: Error inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106654/

相关文章:

javascript - JavaScript XML 文本节点是否有 4096 个字符的限制?

css - XML/XSLT 和 CSS 不显示任何内容

java - 为每个新标记添加标题

android - 谷歌地图 (com.google.android.maps) 使应用程序崩溃

php - 如何使用搜索功能显示具有最接近的 long/lat 值的商店 - Laravel 5 eloquent

android - 使用 DownloadManager 获取 HTTP 错误响应正文

android - 在android中只显示最后一个ArrayList

java - Java AsyncTask 的使用

android - 从 ics 中的库存启动器中删除指向我的应用程序的快捷方式

java - 有条件地在 XML ArrayList 中包含 bean