【问题标题】:android.view.InflateException: Binary XML file line #20: Error inflating class fragmentandroid.view.InflateException: Binary XML file line #20: Error inflating class fragment
【发布时间】:2013-10-07 01:12:23
【问题描述】:

我在 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:二进制 XML 文件第 20 行:膨胀类片段时出错。

<?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 文件中。可以发一下吗?
  • @Szymon - 我已经发布了 XML 文件

标签: android xml google-maps fragment


【解决方案1】:

您不能在 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;
    }

}

就是这样。

【讨论】:

    【解决方案2】:

    在对 stackoverflow 进行研究后,我自己解决了这个问题。 这与嵌套片段问题有关。 我在 xml 中删除了地图片段并为其放置了框架,然后以编程方式添加片段。 它就像一个魅力。 感谢您的所有努力。

    【讨论】:

      【解决方案3】:
      // try this way
      
      public class MapTabMainFragment extends FragmentActivity {
      
          private AdView adView;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.fragment_map_main);
              adView = (AdView) findViewById(R.id.adView1);
              AdRequest re = new AdRequest();
              adView.loadAd(re);
      
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        您正在尝试在 BaseFragment 类中实际创建 Activity 之前访问它...

        mActivity = (BottomTabActivity) this.getActivity();
        

        相反,重写“onActivityCreated”方法并在那里执行...

        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            mActivity = (BottomTabActivity) this.getActivity();
        }
        

        【讨论】:

          【解决方案5】:

          编辑应用程序的 AndroidManifest.xml 文件,并在元素中添加以下声明。这嵌入了用于编译应用的 Google Play 服务版本。

          <meta-data
              android:name="com.google.android.gms.version"
              android:value="@integer/google_play_services_version" />
          

          ->这解决了我的问题。

          【讨论】:

            【解决方案6】:

            不要将xmlns:android="http://schemas.android.com/apk/res/android" 放在Linear Layout 标签中,而是将它放在&lt;com.google.android.gms.ads.AdView 标签中。如下图:

             <com.google.android.gms.ads.AdView 
                    xmlns:ads="http://schemas.android.com/apk/res-auto"
                    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" />

            如需进一步帮助,请访问Link

            【讨论】:

              猜你喜欢
              • 2017-09-13
              • 2015-05-01
              • 2014-06-18
              • 2015-01-13
              • 2013-09-10
              • 2015-04-15
              • 2014-06-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多