我之前尝试了很多次,但始终得到相同的结果。广告显示空白,正如我的标题所示。我不知道我的错误是什么?我从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>
我丢失/忘记了任何步骤吗?我使用所有其他片段和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
答案 0 :(得分:1)
在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。将此文件添加到您的应用程序目录中。
答案 1 :(得分:0)
@Queendevelopers - 收到广告回复时出现问题。 ErrorCode:2。如果日期时间不正确,则会出现错误代码2。
只需在您的设备上设置自动更正即可。另一项测试是改变互联网(从WIfi切换 - >数据,反之亦然)
在相关设备上更新Google Play服务
还要更新到项目中的最新Google Play库。