安卓应用无法运行AdMob

3

我在我的Android应用程序中遇到了AdMob的问题。 我已经完全按照这里所描述的一切来做了,但是当我启动程序时,它无法运行(有很多错误)。我将向您展示程序的一个片段:

public class MenuMainActivity extends Activity{

private AdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_main);  

    adView = new AdView(this, AdSize.BANNER, "(code-15 signs)");
    RelativeLayout layout = (RelativeLayout)findViewById(R.layout.activity_menu_main);
    layout.addView(adView);
    adView.loadAd(new AdRequest());

还有XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/titleOfGame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:text="@string/title_of_game_text" />

当我评论两行代码时:
//layout.addView(adView);
//adView.loadAd(new AdRequest());

一切都好。问题出在哪里?

2个回答

0
如果您使用findViewById(),则必须传递一个id,而您传递的是“layout”。
应该像这样:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);

创建一个名为 "adLayer" 的布局,并将其定位到您希望广告显示的位置。


当我创建布局时,我不知道如何添加任何ID,因为默认情况下只有layout_height、layout_width和tools。在res/values/ids.xml动态添加ID也不是我的好选择。我不知道如何创建一个id为“adLayer”的布局? - andrew
只需在您的布局中添加一个id标签 android:id="@+id/myLayoutName" - mbwasi

0

正如之前的帖子所提到的,您需要在布局中创建一个特定的位置,以便您的广告出现在那里,例如使用您提供的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/titleOfGame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:text="@string/title_of_game_text"/>
<RelativeLayout
    android:id="@+id/adLayer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    etc.   etc./>

然后您需要更改活动中的代码为:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);

正如BrainCrash所建议的那样。 希望这可以帮到你!


好主意,但它仍然不起作用。我已经添加了你写的内容,但问题仍然存在。在“id”类中,“R.java”文件添加了“adLayer”,一切都正常。但是当我启动程序时,会出现错误。我当然已经添加了JAR,并且还覆盖了“onDestroy()”。没有广告一切都正常。 - andrew
你是否尝试将广告在XML文件中指定为视图而不是布局?我发现这样做非常简单。这里有一个很好的例子:http://jmsliu.com/209 - BryanJ
我已经将下载的两个文件GoogleAdMobAdsSdk-6.2.1和libGoogleAnalyticsV2复制到Eclipse项目的"libs"文件夹中。 在activity_main.xml中,我已添加了“android:id="@+id/mainLayout"。 在清单文件中,我已经添加了以下内容: <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 还有 <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> 一切正常。谢谢。 - andrew

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