4

我正在尝试将新的 AdMob 广告添加到我的第一个应用程序中,我设法以正确的方式安排了代码,但我遇到了一些问题:

第一个问题是我的广告 ID ca-app-pub-61413779example出现多个错误,例如:ca无法解析为变量,ba无法解析为变量,int 类型的文字 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

4 回答 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 文件夹下。

于 2013-08-29T21:37:19.890 回答
1

AdView 构造函数需要一个 String 作为第三个参数,因此您基本上缺少双引号:

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

至于您的 XML,请确保您拥有正确的 ID。如果只是mainLayout带下划线,则它不是正确的 ID。如果R带下划线,则表示您的 XML 在某处有错误,并且没有生成 R.java。

于 2013-08-29T21:40:26.623 回答
0

由于您使用 activity_basic_screen.xml 设置内容,因此您必须转到该 xml。

您需要显示 activity_basic_screen.xml 的代码,并告诉您要将广告放在哪里以获取特定详细信息

一般来说,在您的 activity_basic_screen.xml 中,您必须有一个 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”

对我来说,我通常使用 Ahmed 的建议,所以我不需要用 java 编程,但我认为用 java 编程更加动态。

希望有帮助。

PS。在您的屏幕截图中,您涵盖了您的应用程序名称/项目,但您显示了您的包名称。这就像同一件事,因为您可以使用它在 google play 中找到您的应用程序。

于 2013-09-07T00:58:09.803 回答
0

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

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

横幅:尺寸仅为 320x50 智能横幅:尺寸更改为屏幕宽度 x 32|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

希望这有帮助:)

于 2019-05-28T00:54:11.793 回答