用于 2 个 Activity 的通用片段
public class Ads extends Fragment {
private View rootView ;
private MoPubView adView ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.ads, container, true) ;
adView = (MoPubView) rootView.findViewById(R.id.adView) ;
adView.setAdUnitId(LogoQuizUtil.MOPUB_AD_UNIT);
adView.loadAd();
return rootView;
}
}
片段布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ad_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom" >
<com.mopub.mobileads.MoPubView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="bottom" />
</LinearLayout>
我包含此片段的其他布局
<fragment
android:id="@+id/ads"
android:name="myPackage.Ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom" />
当我第一次调用 setContentView (R.layout.ads) 时,我得到一个没有异常,当第二个活动调用 fagementactivity (Ads) 时,我在 setContentView (R.id.ads) 处得到一个异常。
我得到的例外是
08-07 09:38:30.359: W/System.err(975): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.SecondActivity}: android.view.InflateException: Binary XML file line #263: Error inflating class fragment