AdmobError:构造函数AdView(MainActivity.Application,AdSize,String)未定义

时间:2014-02-05 13:17:48

标签: android eclipse sdk admob

我正在尝试实施 google admob sdk ,我收到此错误

The constructor AdView(MainActivity.Application, AdSize, String) is undefined

在此adView = new AdView(this, AdSize.BANNER,

任何帮助?

public class Application extends android.app.Application {

    public Application() {
    }
    public void displayAd() {
        if (displayAd == true) {
            // Create the adView
            try {
                if (adView != null) {
                    adView.destroy();
                }
                adView = new AdView(this, AdSize.BANNER, this.getString(R.string.admob_publisher_id));
                LinearLayout layout = (LinearLayout) findViewById(R.id.adLayout);
                layout.addView(adView);
                adView.loadAd(new AdRequest());
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            LinearLayout layout = (LinearLayout) findViewById(R.id.adLayout);
            layout.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
            layout.setVisibility(View.INVISIBLE);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

而不是

adView = new AdView(this, AdSize.BANNER,this.getString(R.string.admob_publisher_id));

使用

adView = new AdView(this.getApplicationContext(), AdSize.BANNER,this.getString(R.string.admob_publisher_id));

答案 1 :(得分:0)

您正尝试使用应用作为输入创建AdView,但这是无法做到的。 AdView是UI组件,需要在您的Activity的上下文中构建。

将代码转移到Activity并将Activity实例而不是Application传递给AdView构造函数。