'public class AdFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_ad, container, false);
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("some_string")
.build();
mAdView.loadAd(adRequest);
}
@Override
public void onPause() {
super.onPause();
onDestroy();
}
}'
'
<!-- TODO: Update blank fragment layout -->
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/test_banner_ad_unit_id"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</com.google.android.gms.ads.AdView>
</RelativeLayout>'
找不到addTestDevice()
以及如何初始化AdFragment
片段?
在运行此代码时,要么在主屏幕上没有显示任何布局,因为它在项目中没有显示片段,所以我应该在项目中初始化片段。
这是java代码以及xml文件。请帮忙。提前致谢