向Android应用程序添加新AdMob时出现问题

4
我正在尝试将新的 AdMob 广告添加到我的第一个应用程序中。我已经成功按正确方式排列了代码,但是遇到了两个问题:
第一个问题是我的广告 ID ca-app-pub-61413779example 遭受多个错误,例如:ca 无法解析为变量,ba 无法解析为变量,整型字面量61413779 超出范围。
第二个问题是 R.id.mainLayout,即 mainLayout,我不理解。
 protected void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_basic_screen);


     // Create the adView
        adView = new AdView(this, AdSize.BANNER, ca-app-pub-61413779example);

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);


        // Add the adView to it
        layout.addView(adView);
        adView.loadAd(new AdRequest());

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
4个回答

7
我对你的Admob添加方式不熟悉,但我知道另一种仅使用XML将Admob添加到应用程序的好方法。
首先,在您的清单中添加这些权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

* 第二个最重要

然后 在您的清单文件中在 application 标签内添加此内容

<activity android:name="com.google.ads.AdActivity" 
   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

现在,您希望广告出现在每个活动中,您需要将其添加到该活动的layout.xml文件中。

<com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="xxxxxxxxxxxx"
    ads:adSize="BANNER"
    ads:loadAdOnCreate="true" 
    android:layout_gravity="center"
    android:layout_margin="10dp">
</com.google.ads.AdView>

在顶级父布局中(例如LinearLayout),您需要在 xmlns:android="xxx" 下添加这些内容。
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"

现在广告应该可以完美地工作了:D
更新:不要忘记下载并将GoogleAdMobAdsSdk-6.4.1.jar放置在libs文件夹下。

请查看屏幕截图,希望您能帮我解决这个错误。https://www.dropbox.com/s/21lohlqrsiupmb3/4.jpg - Dmitry
你能发布你的activity_basic_screen吗? - Ahmed Ekri
2
非常好而且简单,谢谢Ahmed! - Dmitry

1
AdView构造函数需要一个字符串作为第三个参数,因此您基本上缺少双引号:
adView = new AdView(this, AdSize.BANNER, "ca-app-pub-61413779example");

关于你的XML文件,请确保你使用了正确的ID。如果只有mainLayout被下划线标记,那么它不是正确的ID。如果R被下划线标记,那么这意味着你的XML文件存在错误,R.java文件没有生成。

请看屏幕截图,希望你能帮我摆脱这个错误。[链接](dropbox.com/s/21lohlqrsiupmb3/4.jpg) - Dmitry

0

由于您将内容设置为activity_basic_screen.xml,因此您必须转到该xml文件。

您需要显示activity_basic_screen.xml的代码,并告诉我们要在哪里放置广告以获取特定细节。

一般而言,在您的activity_basic_screen.xml中,您必须拥有一个LinearLayout或其他布局,以显示/填充广告。对于您截图中的问题/LinearLayout,您必须拥有以下内容:

    <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/mainLayout"
     >

你之所以会出现这个错误,是因为我认为你没有使用线性布局。
    android:id="@+id/mainLayout" 

你可以给它取不同的名字

    android:id="@+id/givenName" 

但是你必须在 Java 中将 findViewById 的引用更改为“R.id.givenName”

对我来说,我通常使用艾哈迈德的建议,这样我就不需要在 Java 中编程,但我认为在 Java 中编程更具动态性。

希望能有所帮助。

附言:在您的截图中,您遮盖了应用程序名称/项目,但显示了包名称。这就像是同一件事,因为您可以通过使用该名称在 Google Play 中找到您的应用程序。


0
这可能不是你想要的答案,但我强烈建议使用SMART_BANNER而不是BANNER。在这里查看列出标准横幅尺寸的表格:

https://developers.google.com/admob/android/banner

横幅广告:仅限尺寸320x50 智能横幅广告:尺寸更改为屏幕宽度x32|50|90
以下是如何操作的示例:
-布局
  <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"
       android:id="@+id/main_r"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context="com.example.adexample">


    <RelativeLayout
        android:id="@+id/adView_test"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />



 </android.support.constraint.ConstraintLayout>

-活动:

  public class MainActivity extends AppCompatActivity {

      private AdView adView;
      private AdRequest adRequest;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);


     MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
     admobCall();

  }

 // Function to Set Ads for Main Layout
 private void admobCall(){

    // Set the RelativeLayout id to the view
    View adContainer = findViewById(R.id.adView_test);
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);

    //Real Admob Main Activity Banner
    adView.setAdUnitId(getString(R"YOUR_ADMOB_AD_ID"));

   // Test google ID for Banner
   // adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

   ((RelativeLayout)adContainer).addView(adView);

   // This Request will bulid the ADs as a Test
   //adRequest = new AdRequest.Builder()
   //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

  // This Request Will bulid the ADs as Real
  adRequest = new AdRequest.Builder().build();

  adView.loadAd(adRequest);

 }

另外确保将 "google()" 添加到项目级 build.gradle,并将此实现添加到依赖项中。
implementation 'com.google.android.gms:play-services-ads:17.2.1'

请查看实现说明网站,以确保您没有遗漏任何内容:

https://developers.google.com/admob/android/quick-start

希望这对你有所帮助 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接