我按照说明将admob添加到PhoneGap Android应用程序中,因为它写的是here,但它给了我编译时错误。我有什么需要做的吗?
我得到如下错误:
adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit);
错误:“构造函数AdView(CordovaApp,AdSize,String)未定义”
AdRequest request = new AdRequest();
错误:构造函数AdRequest()不可见
adView.loadAd(request);
错误: AdView类型中的方法loadAd(AdRequest)不适用于参数(AdRequest)
以下完整代码。请帮忙。感谢
package com.example.hello;
import android.os.Bundle;
import android.os.Handler;
import org.apache.cordova.*;
import com.google.ads.*;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.android.gms.ads.*;
import android.widget.LinearLayout;
public class CordovaApp extends CordovaActivity
{
private static final String AdMob_Ad_Unit = "ca-app-pub-7789543298167063/2036477434";
private Handler mHandler = new Handler();
private AdView adView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
mHandler.postDelayed(new Runnable() {
public void run() {
doAdMob();
}
}, 5000);
}
private void doAdMob() {
// Create the adView
adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit);
// Lookup your LinearLayout - get the super.root
LinearLayout layout = super.root;
// Add the adView to it
layout.addView(adView);
// This centers the ads in landscape mode.
layout.setHorizontalGravity(android.view.Gravity.CENTER_HORIZONTAL);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
// and finally...
adView.loadAd(request);
}
}
答案 0 :(得分:0)
基本上,以com.google.ads开头的那些类来自6.4.1或更早版本的Admob。您所指的教程最近两年前更新过。
如果您使用的是Google Play服务中的最新AdMob库,则需要删除要导入的旧类。
替换这些代码
import com.google.ads.*;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.android.gms.ads.*;
与
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdSize;
另外,请尝试按以下方式创建AdRequest:
AdRequest adRequest = new AdRequest.Builder().build();
adview.loadAd(adRequest);
更新:
尝试以下代码
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AdMob_Ad_Unit);