我正在尝试为我的应用设置AdMob横幅。在我的第一个Activity中,我有一个ListView,所以我需要将Banner放在它的底部。我在互联网上搜索了一些东西,我发现了一些东西,不幸的是,它对我不起作用。
我尝试编辑主Activity布局的XML文件,如下所示:
<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>
但LogCat说:
> 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
这是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("");
}
最后,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>
如果我编辑XML,将所有内容放在一个LinearLayout下就可以了,但广告在顶部,弄乱了它下面的标签。我该如何解决?
谢谢你们!
答案 0 :(得分:1)
你的堆栈跟踪非常清楚地告诉你问题:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.pipodi.lolservicestatuseuwonly-1.apk
您尚未将Admob库添加到您的应用中。 见https://developers.google.com/mobile-ads-sdk/docs/
答案 1 :(得分:0)
只要给你的好友提供保证金。
编辑:给你的线性布局一个id,在admob make android:layout_below="@+id/linearlayoutid"
答案 2 :(得分:0)
在如下的LinearLayout中使用android:layout_weight
。
<?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>
答案 3 :(得分:0)
这篇博文应该有所帮助。它甚至有预览。
http://www.ahotbrew.com/how-to-put-admob-banner-on-the-bottom/
这里重要的部分是android:layout_alignParentBottom =“true”用于AdView,而android:layout_alignParentTop =“true”用于LinearLayout之后。
答案 4 :(得分:0)
对于线性布局,您可以像这样添加以显示底部/顶部
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout);
layout.addView(adView,0);
取决于儿童用品,这会有所不同 layout.addView(AD浏览,0); layout.addView(AD浏览,1);