我在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" /> <!-- --> <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; }}
就是这样.
总结以上是内存溢出为你收集整理的android.view.InflateException:二进制XML文件行#20:错误膨胀类片段全部内容,希望文章能够帮你解决android.view.InflateException:二进制XML文件行#20:错误膨胀类片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)