【问题标题】:Runtime error while trying to add admob尝试添加 admob 时出现运行时错误
【发布时间】:2014-11-17 10:01:35
【问题描述】:

当我尝试在完成的应用中添加 admob 时,此错误不断弹出:

11-16 11:20:00.452:E/AndroidRuntime(29463):java.lang.RuntimeException:无法启动活动 ComponentInfo{ch.swegup.lecontroll/ch.swegup.sambadance.MainActivity}:android.view。 InflateException: Binary XML file line #20: Error inflating class com.google.ads.AdView

这是 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="c...y"
    >
    <com.facebook.widget.LikeView
        android:id="@+id/lvLike"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <com.google.ads.AdView 
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"    
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="ca-app-pub-0697264956871753/3628628847E"
        ads:adSize="BANNER"
        ads:loadAdOnCreate="true"
    />

这是java:

import com.google.android.gms.ads.*;
import com.facebook.AppEventsLogger;
import com.facebook.Settings;
import com.facebook.widget.LikeView;
import com.facebook.UiLifecycleHelper;


public class MainActivity extends Activity implements OnClickListener {

    private AdView adView;
    private static final String AD_UNIT_ID = "ca-app-pub-0697264956871753/3628628847E";

    //UiLifecycleHelper uiHelper;
    TextView textView;
//  Button buttonende;
    Button tipps;
    Button btn1;
    Button rate;

    LikeView LikeView;

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

        textView = (TextView) findViewById(R.id.textView);
        tipps = (Button) findViewById(R.id.tipps);
        btn1 = (Button) findViewById(R.id.buttonSTART);
        btn1.setOnClickListener(this);
        rate = (Button) findViewById(R.id.rate);
        rate.setOnClickListener(this);

        // Create the adView.
        adView = (AdView) findViewById(R.id.adView);



        // Lookup your RelativeLayoutLayout assuming it’s been given     
        // the attribute android:id="@+id/ad"     

        // Initiate a generic request to load it with an ad     
        AdRequest adRequest = new AdRequest.Builder().build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

【问题讨论】:

    标签: java android admob


    【解决方案1】:

    这是因为您将 com.google.ads.AdView 与 com.google.android.gms.ads.AdView 混淆了。您在 XML 中请求了一个,但在您的代码中期望另一个。

    您应该对两者都使用 com.google.android.gms.ads.AdView,因为这是来自 GooglePlayServices 的 AdView。

    【讨论】:

      【解决方案2】:

      将创建 adView 的代码更改为:

      adView = (AdView) findViewById(R.id.adView);
      

      并删除以下行:

      RelativeLayout layout = (RelativeLayout)findViewById(R.id.adView);     
      layout.addView(adView);
      

      更新

      尝试将您的 xml 标签从 com.google.android.Adview 更改为

      <com.google.android.gms.ads.AdView
      

      此外,您应该删除 loadAdOnCreate 属性,因为它在 Google Play 服务中不再可用。

      参考:https://developers.google.com/mobile-ads-sdk/docs/admob/android/play-migration

      希望这会有所帮助。

      【讨论】:

      • @EmanuelGraf 您应该使用 Google Play 服务,而不是 Android SDK。我更新了我的答案。
      • 它工作得很好。但现在我收到此错误:11-16 12:29:15.902: W/Ads(2944): 获得广告响应时出现问题。错误代码:1 11-16 12:29:15.952:W/Ads(2944):无法加载广告:1
      • @EmanuelGraf 很高兴听到它有效。错误代码 1 是 ERROR_CODE_INVALID_REQUEST,这意味着广告单元 ID 不正确。检查您的广告单元 ID 是否有效。
      • thx dude :DD 现在还有一个问题:此广告的代码是否稳定?我的意思是,我今天开始宣传这个应用程序,如果它不起作用,它会有点失败......所以问题是:我可以将这个版本推广到 100% 吗?
      • 嗯..我不能说,因为我没有看到你的应用程序的完整源代码。确保在市场上发布之前对您的应用进行足够的测试。如果您的代码运行良好,请接受我的回答:)
      【解决方案3】:

      我在尝试自己实现 admob 时遇到了这里的优秀小教程,我认为这是迄今为止最好和最简单的方法。

      http://blog.blundell-apps.com/migrating-to-admob-in-google-play-services-from-admob-sdk-jar/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-18
        • 2013-09-17
        • 2017-04-04
        • 2018-05-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多