尝试添加admob时出现运行时错误

时间:2014-11-16 10:25:50

标签: java android admob

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

11-16 11:20:00.452:E / AndroidRuntime(29463):java.lang.RuntimeException:无法启动活动ComponentInfo {ch.swegup.lecontroll / ch.swegup.sambadance.MainActivity}:android.view。 InflateException:二进制XML文件行#20: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);

3 个答案:

答案 0 :(得分:1)

将创建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

希望这有帮助。

答案 1 :(得分:1)

这是因为com.google.ads.AdView与com.google.android.gms.ads.AdView相混淆。您已经在XML中要求使用其中一个,但希望代码中有另一个。

您应该同时使用com.google.android.gms.ads.AdView,因为这是来自GooglePlayServices的AdView。

答案 2 :(得分:1)

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

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