Я вставляю AdMob в свое приложение. но у меня проблема с этим. Я пытаюсь показать баннер Admob во фрагменте. Когда я запускаю код ниже, он ничего не показывает и не дает никаких ошибок, я не понимаю, в чем проблема и почему он не запускается, как я могу правильно использовать Admob в Фрагмент и как показать баннер Admob во фрагменте?
Фрагмент
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialogs, container, false);
MobileAds.initialize(getContext(), getString(R.string.admob_app_id));
mAdView = (AdView) rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Inflate the layout for this fragment
return rootView;
}
fragment_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_layout"
tools:context=".DialogsFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container_body">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/container_items"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="none"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:text="@string/dialog_title"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
app:adSize="BANNER"
app:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/boslyt"
android:visibility="visible"
android:layout_marginTop="90dp"
android:orientation="vertical">
<ImageView
android:id="@+id/splash"
android:layout_width="170dp"
android:layout_height="140dp"
app:srcCompat="@drawable/ic_nochat" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="50dp"
android:paddingTop="15dp"
android:paddingRight="50dp"
android:text="@string/label_nochat"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/grey_60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/boslyt"
android:gravity="center"
android:id="@+id/chtlyt"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:id="@+id/chattext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:visibility="visible"
app:srcCompat="@drawable/yon_chat" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Манифест
<!-- AdMob App ID -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_app_id"/>