Admob 横幅广告显示空白?为什么?
Admob Banner ad showing blank? Why?
我以前试过很多次,但总是得到相同的结果。正如我的标题所说,广告显示空白。我不知道我出了什么问题?我向 here 学习。下面是我的代码和屏幕截图:
这是我的 activity_main.xml,我在页脚
投放了广告
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/fragment_container">
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="@+id/img_boy"
android:background="@drawable/big_img_boy" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="2"
android:orientation="horizontal">
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/dog_male"
android:scaleType="centerCrop"
android:background="@drawable/one" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/dog_female"
android:scaleType="centerCrop"
android:background="@drawable/two" />
</LinearLayout>
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="@+id/img_girl"
android:background="@drawable/original_girl" />
<com.google.android.gms.ads.AdView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-2679xxxxxxxxxxxxxxxx42"
android:id="@+id/adView"
>
</com.google.android.gms.ads.AdView>
</LinearLayout>
在 MainActivity 中,我在 onCreate 中调用它
AdView adView= (AdView) findViewById(R.id.adView);
AdRequest adRequest=new AdRequest.Builder().build();
adView.loadAd(adRequest);
这是我的 manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="queendevelopers.com.nepalibabiesnames">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/launcher_icon"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|locale"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".Splash_Screen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"></activity>
<activity android:name=".Settings"
android:screenOrientation="portrait"></activity>
<activity android:name=".About_Developer"
android:screenOrientation="portrait"/>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
我 lose/forgot 有任何步骤吗?我正在使用所有其他片段和 activity_main 作为容器,当我去片段时,这个横幅自动显示在顶部而不是显示在底部。下面是屏幕截图。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="vertical"
app:cardBackgroundColor="@android:color/transparent"
app:cardCornerRadius="8dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="3dp"
android:textAlignment="center"
android:textColor="#ff9901"
android:textSize="35sp"
android:textStyle="italic" />
</android.support.v7.widget.CardView>
点击按钮时在 ManActivity 中调用它们,如下代码所示:-
case R.id.img_boy:
Recycler rc = new Recycler();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, rc);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
下面是我如何膨胀我的视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recyclerview, container, false);
rv = (RecyclerView) view.findViewById(R.id.rv);
nameCollection = getActivity().getResources().getStringArray(R.array.boy);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeData();
initializeAdapter();
return view;
下面是logcat
08-31 17:23:24.208 25652-25724/? D/HeadsetStateMachine: Disconnected process message: 10, size: 0
08-31 17:24:43.512 15331-15331/queendevelopers.com.nepalibabiesnames D/DynamitePackage: Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
08-31 17:24:43.929 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Starting ad request.
08-31 17:24:43.932 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Use AdRequest.Builder.addTestDevice("0F7B337499FCAAA05252B09EFC57223C") to get test ads on this device.
08-31 17:24:44.994 2526-2526/? I/Ads: Received log message: <Google:HTML> You are using version 8.5 of the Google Play Services Ads SDK. Please consider updating your SDK to the most recent SDK version to get the latest features and bug fixes. See [link][4] for instructions on how to get the latest version of Google Play services.
08-31 17:24:45.267 2526-5326/? W/Ads: Error while connecting to ad server: Connection closed by peer
08-31 17:24:45.272 15331-15352/queendevelopers.com.nepalibabiesnames W/Ads: There was a problem getting an ad response. ErrorCode: 2
08-31 17:24:45.278 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Scheduling ad refresh 60000 milliseconds from now.
08-31 17:24:45.278 15331-15331/queendevelopers.com.nepalibabiesnames W/Ads: Failed to load ad: 2
08-31 17:25:45.337 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Starting ad request.
08-31 17:25:45.339 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Use AdRequest.Builder.addTestDevice("0F7B337499FCAAA05252B09EFC57223C") to get test ads on this device.
08-31 17:25:46.775 2526-5324/? W/Ads: Error while connecting to ad server: Connection closed by peer
08-31 17:25:46.777 15331-15352/queendevelopers.com.nepalibabiesnames W/Ads: There was a problem getting an ad response. ErrorCode: 2
08-31 17:25:46.778 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Scheduling ad refresh 60000 milliseconds from now.
08-31 17:25:46.779 15331-15331/queendevelopers.com.nepalibabiesnames W/Ads: Failed to load ad: 2
08-31 17:26:46.835 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Starting ad request.
08-31 17:26:46.836 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Use AdRequest.Builder.addTestDevice("0F7B337499FCAAA05252B09EFC57223C") to get test ads on this device.
08-31 17:26:47.185 2526-2526/? I/Ads: Received log message: <Google:HTML> You are using version 8.5 of the Google Play Services Ads SDK. Please consider updating your SDK to the most recent SDK version to get the latest features and bug fixes. See [link][4] for instructions on how to get the latest version of Google Play services.
08-31 17:26:47.642 2526-5322/? W/Ads: Error while connecting to ad server: Connection closed by peer
08-31 17:26:47.645 15331-15352/queendevelopers.com.nepalibabiesnames W/Ads: There was a problem getting an ad response. ErrorCode: 2
08-31 17:26:47.646 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Scheduling ad refresh 60000 milliseconds from now.
08-31 17:26:47.648 15331-15331/queendevelopers.com.nepalibabiesnames W/Ads: Failed to load ad: 2
在您的 MainActivity 中使用它
RelativeLayout adContainer = (RelativeLayout)
findViewById(R.id.adMobView);
AdView mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
adContainer.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
将此添加到您的 main.xml
<RelativeLayout
android:id="@+id/adMobView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
</RelativeLayout>
另请记住,横幅广告需要(宽度、高度 320X50)。
在 buid.gradle
中实现以下内容
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.firebase:firebase-ads:9.0.0'
compile 'com.android.support:design:23.4.0'
}
从 firebase 控制台获取 google-services.json。将此文件添加到您的应用程序目录中。
@Queendevelopers - 获取广告响应时出现问题。错误代码:2。如果日期时间不正确,则会出现错误代码 2。
只需在您的设备上设置自动日期校正。另一个测试是改变互联网(从WIfi切换 - >数据或反之亦然)
更新 Google 在有问题的设备上播放服务
同时更新到项目中最新的 Google 播放库。
我以前试过很多次,但总是得到相同的结果。正如我的标题所说,广告显示空白。我不知道我出了什么问题?我向 here 学习。下面是我的代码和屏幕截图:
这是我的 activity_main.xml,我在页脚
投放了广告<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/fragment_container">
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="@+id/img_boy"
android:background="@drawable/big_img_boy" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="2"
android:orientation="horizontal">
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/dog_male"
android:scaleType="centerCrop"
android:background="@drawable/one" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/dog_female"
android:scaleType="centerCrop"
android:background="@drawable/two" />
</LinearLayout>
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:id="@+id/img_girl"
android:background="@drawable/original_girl" />
<com.google.android.gms.ads.AdView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-2679xxxxxxxxxxxxxxxx42"
android:id="@+id/adView"
>
</com.google.android.gms.ads.AdView>
</LinearLayout>
在 MainActivity 中,我在 onCreate 中调用它
AdView adView= (AdView) findViewById(R.id.adView);
AdRequest adRequest=new AdRequest.Builder().build();
adView.loadAd(adRequest);
这是我的 manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="queendevelopers.com.nepalibabiesnames">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/launcher_icon"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|locale"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".Splash_Screen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"></activity>
<activity android:name=".Settings"
android:screenOrientation="portrait"></activity>
<activity android:name=".About_Developer"
android:screenOrientation="portrait"/>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
我 lose/forgot 有任何步骤吗?我正在使用所有其他片段和 activity_main 作为容器,当我去片段时,这个横幅自动显示在顶部而不是显示在底部。下面是屏幕截图。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="vertical"
app:cardBackgroundColor="@android:color/transparent"
app:cardCornerRadius="8dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="3dp"
android:textAlignment="center"
android:textColor="#ff9901"
android:textSize="35sp"
android:textStyle="italic" />
</android.support.v7.widget.CardView>
点击按钮时在 ManActivity 中调用它们,如下代码所示:-
case R.id.img_boy:
Recycler rc = new Recycler();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, rc);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
下面是我如何膨胀我的视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recyclerview, container, false);
rv = (RecyclerView) view.findViewById(R.id.rv);
nameCollection = getActivity().getResources().getStringArray(R.array.boy);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeData();
initializeAdapter();
return view;
下面是logcat
08-31 17:23:24.208 25652-25724/? D/HeadsetStateMachine: Disconnected process message: 10, size: 0
08-31 17:24:43.512 15331-15331/queendevelopers.com.nepalibabiesnames D/DynamitePackage: Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
08-31 17:24:43.929 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Starting ad request.
08-31 17:24:43.932 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Use AdRequest.Builder.addTestDevice("0F7B337499FCAAA05252B09EFC57223C") to get test ads on this device.
08-31 17:24:44.994 2526-2526/? I/Ads: Received log message: <Google:HTML> You are using version 8.5 of the Google Play Services Ads SDK. Please consider updating your SDK to the most recent SDK version to get the latest features and bug fixes. See [link][4] for instructions on how to get the latest version of Google Play services.
08-31 17:24:45.267 2526-5326/? W/Ads: Error while connecting to ad server: Connection closed by peer
08-31 17:24:45.272 15331-15352/queendevelopers.com.nepalibabiesnames W/Ads: There was a problem getting an ad response. ErrorCode: 2
08-31 17:24:45.278 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Scheduling ad refresh 60000 milliseconds from now.
08-31 17:24:45.278 15331-15331/queendevelopers.com.nepalibabiesnames W/Ads: Failed to load ad: 2
08-31 17:25:45.337 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Starting ad request.
08-31 17:25:45.339 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Use AdRequest.Builder.addTestDevice("0F7B337499FCAAA05252B09EFC57223C") to get test ads on this device.
08-31 17:25:46.775 2526-5324/? W/Ads: Error while connecting to ad server: Connection closed by peer
08-31 17:25:46.777 15331-15352/queendevelopers.com.nepalibabiesnames W/Ads: There was a problem getting an ad response. ErrorCode: 2
08-31 17:25:46.778 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Scheduling ad refresh 60000 milliseconds from now.
08-31 17:25:46.779 15331-15331/queendevelopers.com.nepalibabiesnames W/Ads: Failed to load ad: 2
08-31 17:26:46.835 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Starting ad request.
08-31 17:26:46.836 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Use AdRequest.Builder.addTestDevice("0F7B337499FCAAA05252B09EFC57223C") to get test ads on this device.
08-31 17:26:47.185 2526-2526/? I/Ads: Received log message: <Google:HTML> You are using version 8.5 of the Google Play Services Ads SDK. Please consider updating your SDK to the most recent SDK version to get the latest features and bug fixes. See [link][4] for instructions on how to get the latest version of Google Play services.
08-31 17:26:47.642 2526-5322/? W/Ads: Error while connecting to ad server: Connection closed by peer
08-31 17:26:47.645 15331-15352/queendevelopers.com.nepalibabiesnames W/Ads: There was a problem getting an ad response. ErrorCode: 2
08-31 17:26:47.646 15331-15331/queendevelopers.com.nepalibabiesnames I/Ads: Scheduling ad refresh 60000 milliseconds from now.
08-31 17:26:47.648 15331-15331/queendevelopers.com.nepalibabiesnames W/Ads: Failed to load ad: 2
在您的 MainActivity 中使用它
RelativeLayout adContainer = (RelativeLayout)
findViewById(R.id.adMobView);
AdView mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
adContainer.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
将此添加到您的 main.xml
<RelativeLayout
android:id="@+id/adMobView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
</RelativeLayout>
另请记住,横幅广告需要(宽度、高度 320X50)。 在 buid.gradle
中实现以下内容dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.firebase:firebase-ads:9.0.0'
compile 'com.android.support:design:23.4.0'
}
从 firebase 控制台获取 google-services.json。将此文件添加到您的应用程序目录中。
@Queendevelopers - 获取广告响应时出现问题。错误代码:2。如果日期时间不正确,则会出现错误代码 2。
只需在您的设备上设置自动日期校正。另一个测试是改变互联网(从WIfi切换 - >数据或反之亦然)
更新 Google 在有问题的设备上播放服务
同时更新到项目中最新的 Google 播放库。