Admob广告未显示在我的Android应用中

时间:2019-06-12 20:28:59

标签: android android-fragments admob

我通过google admob控制台创建了广告-甚至还算了请求
我已连接到Firebase
我已按照所有说明在我的android应用中添加了admob广告,但没有显示。
怎么了?
抱歉-很多代码,但是我不明白在哪里挖

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.view.MainActivity">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/main_container"
        android:layout_centerHorizontal="true"
        android:background="@color/white"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_id" />

    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/adView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/adView"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>  

MainActivity.java

public class MainActivity extends AppCompatActivity {
AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MobileAds.initialize(this, String.valueOf(R.string.google_app_id));
    //adding first visible fragment 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    Fragment mainScreenFragment = new MainScreenFragment();
    //adding banner
    adView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("7B0491EFFC06B1A3FBE6637394533EDE")
            .build();
    adView.loadAd(adRequest);
    String tag = "MainScreen";
    transaction.replace(R.id.main_container, mainScreenFragment, tag);
    transaction.commit();
}  

第一个可见片段(MainScreenFragment.java的xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.view.MainScreenFragment">
    <Button
        android:id="@+id/btn_pay"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:background="@drawable/round_button"
        android:textAlignment="center"
        android:textColor="#000"
        android:textSize="18sp" />
          ....
</RelativeLayout>

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.appolinary.antiradar">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:name="com.appolinary.antiradar.App"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="@string/google_app_id"/>
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true" />
    <activity
        android:name=".presentation.view.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

渐变

//admob
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-ads:17.2.1'
implementation 'com.google.android.gms:play-services-ads:17.2.1'
implementation 'com.google.android.gms:play-services-measurement-sdk-api:16.5.0'

1 个答案:

答案 0 :(得分:0)

删除gradle中的一种实现(在firebase-ads:17.2.1,play-services-ads:17.2.1之间选择(因为不应该在一起))。当应用开始发出更多请求以评估您的流量时,Admob就会开始向您展示广告。另外,请尝试在不显示广告的设备上的设置中重置广告ID。