I'm trying to set an AdMob Banner for my app. In my first Activity I've got a ListView, so I need to put the Banner on the bottom of it. I've searched for something on the internet, and I've found something but, unfortunately, it doesn't work for me.

I tried to edit the XML file of the main Activity layout, like this:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
    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=".MainActivity" >



    <ListView
       android:id="@+id/main_list"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>
    <com.google.android.gms.ads
    android:layout_alignParentBottom="true">
    </com.google.android.gms.ads>
</RelativeLayout>

But the LogCat says:

> 11-27 01:36:21.783: E/AndroidRuntime(19734): FATAL EXCEPTION: main
>11-27 01:36:21.783: E/AndroidRuntime(19734): java.lang.RuntimeException: Unable to start >activity ComponentInfo{com.pipodi.lolservicestatuseuwonly/com.pipodi.lolservicestatuseuwonly.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class com.google.ads.AdView
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.ActivityThread.access$600(ActivityThread.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.os.Handler.dispatchMessage(Handler.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.os.Looper.loop(Looper.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.ActivityThread.main(ActivityThread.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at java.lang.reflect.Method.invokeNative(Native Method)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at java.lang.reflect.Method.invoke(Method.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at dalvik.system.NativeStart.main(Native Method)
>11-27 01:36:21.783: E/AndroidRuntime(19734): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class com.google.ads.AdView
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.view.LayoutInflater.rInflate(LayoutInflater.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.view.LayoutInflater.inflate(LayoutInflater.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.view.LayoutInflater.inflate(LayoutInflater.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.view.LayoutInflater.inflate(LayoutInflater.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.Activity.setContentView(Activity.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at com.pipodi.lolservicestatuseuwonly.MainActivity.onCreate(MainActivity.java:43)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.Activity.performCreate(Activity.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   ... 12 more
>11-27 01:36:21.783: E/AndroidRuntime(19734): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.pipodi.lolservicestatuseuwonly-1.apk
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at java.lang.ClassLoader.loadClass(ClassLoader.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at java.lang.ClassLoader.loadClass(ClassLoader.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   at android.view.LayoutInflater.createView(LayoutInflater.java)
>11-27 01:36:21.783: E/AndroidRuntime(19734):   ... 22 more

This is the method OnCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        // Create the adView.
        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-9509002681827594/8300786666");
        adView.setAdSize(AdSize.BANNER);

        LinearLayout layout = (LinearLayout)findViewById(R.id.main);

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

        // Initiate a generic request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Load the adView with the ad request.
        adView.loadAd(adRequest);

        super.onCreate(savedInstanceState);
        c=this.getApplicationContext();
        list = (ListView)this.findViewById(R.id.main_list);
        ParsingPaginaWeb parsing = new ParsingPaginaWeb();
        parsing.execute("");
    }

And the last, the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pipodi.lolservicestatuseuwonly"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>
        <activity
            android:name="com.pipodi.lolservicestatuseuwonly.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.pipodi.lolservicestatuseuwonly.PostCatcher"
            android:label="@string/title_activity_post_catcher" 
            android:parentActivityName="com.pipodi.lolservicestatuseuwonly.MainActivity">
        </activity>
        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
</manifest>

If I edit the XML, putting all the stuff under a single LinearLayout it works, but the Ad is on top, messing up with the tab under it. How can I solve it?

Thanks guys!

有帮助吗?

解决方案

Your stacktrace quite clearly gives you the problem:

 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.pipodi.lolservicestatuseuwonly-1.apk

You haven't added the Admob library to your app. See https://developers.google.com/mobile-ads-sdk/docs/

其他提示

Just give margin bottom to your admob.. Edit: give a id to your linear layout , in admob make android:layout_below="@+id/linearlayoutid"

Use android:layout_weight in a LinearLayout like below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/main_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.google.ads.AdView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        ads:adUnitId="*****"
        ads:adSize="SMART_BANNER"
        ads:loadAdOnCreate="true"/>

</LinearLayout>

This blog post should help. It even has a preview.

http://www.ahotbrew.com/how-to-put-admob-banner-on-the-bottom/

The important part here are android:layout_alignParentBottom=”true” for the AdView and android:layout_alignParentTop=”true” for the LinearLayout afterwards.

For linear layout you can add like this to show bottom/top

 LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout);
        layout.addView(adView,0);

depending upon the child items this varies layout.addView(adView,0); layout.addView(adView,1);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top