我正在尝试在应用程序中投放广告,但由于该应用程序编译正常,我遇到了麻烦,并且可以在android studio预览中看到添加的内容,但在应用程序中看不到添加的内容:
Image of the app in the previewer 和 Image of the app in the emulator 这是布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground_frases"
tools:context="com.fire.app.yonunca.Frases_Activity"
android:screenOrientation="portrait"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="49dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="30"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal|center"
>
<TextView
android:id="@+id/frases_tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yonunca"
android:textColor="#ffffff"
android:textSize="45dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="50"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal|center"
>
<TextView
android:id="@+id/frase_aleatoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/frase_aleatoria"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical|center_horizontal|center"
android:textColor="#ffffff"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="20"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal|center"
>
<Button
android:id="@+id/button_siguiente"
android:layout_width="200dip"
android:layout_height="40dip"
android:theme="@style/NoAddsButton"
android:text="SIGUIENTE"
android:onClick="onClickNext"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="10"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal|center"
>
<com.google.android.gms.ads.AdView
android:id="@+id/av_bottom_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_inferior_ad_unit_id" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
活动
package com.fire.app.yonunca;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import java.util.Random;
public class Frases_Activity extends AppCompatActivity {
TextView frase_aleatoria;
CrossInfo crossinfo = new CrossInfo();
int gamemode = crossinfo.getGameMode();
Repositorio repositorio = new Repositorio();
String frase_show = "me he enamorado";
Random rand = new Random();
int random_type = rand.nextInt(2) + 1;
//2 is the maximum and the 1 is our minimum.
private AdView mBottomBanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frases_);
frase_aleatoria = (TextView)findViewById(R.id.frase_aleatoria);
getFrase();
// Ui
mBottomBanner = (AdView) findViewById(R.id.av_bottom_banner);
getdeviceID_Ad();
}
public void getdeviceID_Ad() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mBottomBanner.loadAd(adRequest);
}
public void askforAd(){
//El numero ese corresponde al id de mi movil
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("DEVICE_ID_EMULATOR")
.build();
mBottomBanner.loadAd(adRequest);
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fire.app.yonunca">
<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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppThemeNoBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Frases_Activity" />
<activity android:name=".Main2Activity"></activity>
</application>
</manifest>
builld.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.fire.app.yonunca"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.firebase:firebase-ads:16.0.1'
compile 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
答案 0 :(得分:0)
我正在阅读日志猫,发现错误是:
广告加载失败:3
总而言之,这意味着代码还可以,但是问题是admob目前没有适合我的广告。有一些解决方案,例如在admob控制台中创建新的广告位置。
如果有人到这里遇到同样的问题,您可以在这里找到更多信息:
Admob ad request always return code 3
再见!