我是Android开发人员的新手,所以我尝试在我的应用上集成广告,但有些错误即将到来。您可以告诉我如何在App.Thanks上正确添加代码行广告
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.adbuddiz.ads"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:background="@drawable/hanumanji1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HanumanActivity" >
<Button
android:id="@+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/circle_shape_drawable"
android:text="@string/pau"
android:textColor="#ffff00" />
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/circle_shape_drawable"
android:gravity="center"
android:text="@string/pl"
android:textColor="#ffff00" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/circle_shape_drawable"
android:gravity="center"
android:text="@string/de"
android:textColor="#ffff00" />
<com.google.ads.AdView
android:id="@+id/adView"
ads:adUnitId="xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize= "BANNER"
ads:testDevices="TEST_EMULATER"
ads:LoadAdOnCreate="true"/>
</RelativeLayout>
这是Java类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hanuman);
AdBuddiz.setLogLevel(AdBuddizLogLevel.Info); // log level
AdBuddiz.setPublisherKey("a91379f7-dba2-4bd4-a60b-92f0b9700f3d"); // replace with your app publisher key
AdBuddiz.setTestModeActive(); // to delete before submitting to
AdBuddiz.cacheAds(this); // this = current Activity
Logcat错误
04-01 04:07:40.330: E/AndroidRuntime(1778): FATAL EXCEPTION: main
04-01 04:07:40.330: E/AndroidRuntime(1778): Process: e.hanuman, PID: 1778
04-01 04:07:40.330: E/AndroidRuntime(1778): java.lang.RuntimeException: Unable to start activity ComponentInfo{e.hanuman/e.hanuman.HanumanActivity}: android.view.InflateException: Binary XML file line #42: Error inflating class com.google.ads.AdView
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.os.Looper.loop(Looper.java:136)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-01 04:07:40.330: E/AndroidRuntime(1778): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 04:07:40.330: E/AndroidRuntime(1778): at java.lang.reflect.Method.invoke(Method.java:515)
04-01 04:07:40.330: E/AndroidRuntime(1778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-01 04:07:40.330: E/AndroidRuntime(1778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-01 04:07:40.330: E/AndroidRuntime(1778): at dalvik.system.NativeStart.main(Native Method)
04-01 04:07:40.330: E/AndroidRuntime(1778): Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class com.google.ads.AdView
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-01 04:07:40.330: E/AndroidRuntime(1778): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.Activity.setContentView(Activity.java:1929)
04-01 04:07:40.330: E/AndroidRuntime(1778): at e.hanuman.HanumanActivity.onCreate(HanumanActivity.java:72)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.Activity.performCreate(Activity.java:5231)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-01 04:07:40.330: E/AndroidRuntime(1778): ... 11 more
04-01 04:07:40.330: E/AndroidRuntime(1778): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: DexPathList[[zip file "/data/app/e.hanuman-1.apk"],nativeLibraryDirectories=[/data/app-lib/e.hanuman-1, /vendor/lib, /system/lib]]
04-01 04:07:40.330: E/AndroidRuntime(1778): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-01 04:07:40.330: E/AndroidRuntime(1778): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
04-01 04:07:40.330: E/AndroidRuntime(1778): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
04-01 04:07:40.330: E/AndroidRuntime(1778): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
04-01 04:07:40.330: E/AndroidRuntime(1778): ... 21 more
答案 0 :(得分:0)
// Try this code just add this activity and use it's SetupAds() method where u want to display add mob
package com.YourPackageName;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.app.Activity;
public class AdMobMainActivity extends Activity implements AdListener
{
public AdView adView;
public String ADV_PUB_ID = "Your Key";
private boolean adVisible = true;
LinearLayout layout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Admob", "Calling External");
}
public void SetupAds(){
Log.i("AdMob", "Start Setup");
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(android.view.Gravity.BOTTOM | android.view.Gravity.CENTER_HORIZONTAL); //To put AdMob Adv to Bottom of Screen
Log.i("AdMob", "End Layout Setup");
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
adView = new AdView(this, AdSize.BANNER, ADV_PUB_ID);
adView.setAdListener(this);
Log.i("AdMob", "Init complete Adview");
layout.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("AdMob", "Done AddView Layout");
AdRequest request = new AdRequest();
request.addTestDevice("Your Device Id");
//request.addTestDevice(AdRequest.TEST_EMULATOR);
request.addKeyword("LifeOK");
adView.loadAd(new AdRequest());
Log.i("AdMob", "End Setup");
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0: //Disable Adv
if (adVisible)
adVisible = false;
break;
case 1: //Enable Adv
if (!adVisible)
{
Log.i("AdMob", "Case 1");
adVisible = true;
}
break;
case 2: //Enable but Hide Adv
adView.setVisibility(View.GONE);
break;
case 3: //Enable but Show Adv
adView.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
};
public void DisableAds()
{
Log.i("AdMob", "Request Disable Adv");
handler.sendEmptyMessage(0);
}
public void EnableAds()
{
Log.i("AdMob", "Request Enable Adv");
handler.sendEmptyMessage(1);
}
public void HideAdv() //Enable Adv but Hide
{
Log.i("AdMob", "Request Hide Adv");
handler.sendEmptyMessage(2);
}
public void ShowAdv() //Show Adv
{
Log.i("AdMob", "Request Show Adv");
handler.sendEmptyMessage(3);
}
@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Dismiss Screen");
}
@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
Log.d("AdMob", "failed to receive ad (" + arg1 + ")");
}
@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Leaving Application");
}
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Present Screen");
}
@Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Adv Received");
}
}
//并在你的oncreate方法中添加SetupAds()&#34;