【问题标题】:AdmobError:The constructor AdView(MainActivity.Application, AdSize, String) is undefinedAdmobError:构造函数 AdView(MainActivity.Application, AdSize, String) 未定义
【发布时间】:2014-02-05 13:17:47
【问题描述】:

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

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

在此广告视图上 = 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);
        }
    }
}

【问题讨论】:

  • 您是否在任何位置声明了 adView?
  • 使用您的活动名称代替 AdView(this....):AdView(MainActivity.this...)

标签: android eclipse sdk admob


【解决方案1】:

代替

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));

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多