请帮助一些东西,我试图在应用程序上添加admobs但它在我运行它时不会显示在应用程序上,但它出现在android工作室预览中 . 我该如何解决这个问题?
此代码位于主活动布局上
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/relatively"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d15400"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="@+id/linearLayout11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Score : 1"
android:textColor="#ffffff"
android:textSize="25.0sp"
android:textStyle="bold" />
<TextView
android:id="@+id/timers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="00:00:49"
android:textColor="#ffffff"
android:textSize="25.0sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.40" >
<TextView
android:id="@+id/txtQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="15*2*1-1"
android:textColor="#ffffff"
android:textSize="35.0sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="vertical"
android:weightSum="1" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:text="30"
android:textColor="#000000"
android:textSize="25.0sp" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:text="29"
android:textColor="#000000"
android:textSize="25.0sp" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:text="32"
android:textColor="#000000"
android:textSize="25.0sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<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_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</LinearLayout>
此代码在问题活动类上 -
setContentView(R.layout.activity_main);
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
这是清单上的cose
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hitesh.asimplegame">
<!-- Include required permissions for Google Mobile Ads to run. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".QuestionActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><![CDATA[
android:name=".ResultActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
]]>
<activity android:name=".Finish" />
<activity android:name=".SplashScrean" />
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" />
</application>
</manifest>
这是资源
resources>
<string name="app_name">A simple game</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_admobs">admobs</string>
<!-- -
This is an ad unit ID for a banner test ad. Replace with your own banner ad unit id.
For more information, see https://support.google.com/admob/answer/3052638
<!- -->
<string name="banner_ad_unit_id">4526809957646802/687614978</string>
<string name="title_activity_main">MainActivity</string>6
<string name="interstitial_ad_sample">Interstitial Ad Sample</string>
<string name="start_level">Level 1</string>
<string name="next_level">Next Level</string>
<!-- -
This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id.
For more information, see https://support.google.com/admob/answer/3052638
<!- -->
<string name="interstitial_ad_unit_id">4526809957646802/6876146978</string>
<string name="title_activity_doa">doa</string>
</resources>
3 回答
您的广告ID应以ca-app-pub-your_key开头,其中包含ID, Banner ID以及插页式ID
所以使用这些:
代码没有问题 . 问题是,您可能会在几分钟前创建该广告单元ID . 因此,它需要更多时间才能出现在真实设备中 . 如果您真的想知道代码是否正常,请将 Headers ID设置为
ca-app-pub-3940256099942544/6300978111
. 这是一个预览ID,它将适合您 .