android - 自定义对话框上的 Admob 广告

标签 android dialog admob

我正在尝试在应用程序上实现 Admob 广告。我想要做的是在自定义对话框中转换横幅广告。我已经尝试了所有方法,但找不到解决方案。

我为对话框制作了自定义 xml。在 xml 上添加 admob 时,它不会运行。所以我尝试以编程方式进行。但仍然不能让它发挥作用。

public void OnClickButton(View paramView)
  {
    int btn_id = paramView.getId();

       if (btn_id == R.id.hint_field)
       {
     //set up dialog
           if (hint != null && hint.length()>0) {
          final Dialog dialog = new Dialog(Activity.this);
          dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);

          //ad loading
          dialog.setContentView(R.layout.custom_dialog);
          RelativeLayout layout = (RelativeLayout)findViewById(R.id.dialog_l);
          layout.addView(ad);
          AdRequest r = new AdRequest();
          ad.loadAd(r);

          dialog.setTitle("Σχετικά με την λέξη :");
          dialog.setCancelable(true);

           //set up text
          TextView text = (TextView) dialog.findViewById(R.id.hint_text);
          text.setText(hint);



         //set up button
           Button button = (Button) dialog.findViewById(R.id.Button01);
           button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
                   dialog.dismiss();
              }
          });

          // now that the dialog is set up, it's time to show it    
           dialog.show();
           dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
     }
   }   

在单击按钮时,我会显示一个自定义对话框。在我的 Activity 的 OnCreate 方法中,我制作了 adView

  AdView  ad = new AdView(this, AdSize.BANNER, "a15xxxxxxxxxxx");

我在 layout.addView(ad); 处得到一个 NullPointerException

有什么想法吗?

提前致谢!

最佳答案

问题解决了!我必须先膨胀自定义对话框界面。下面的代码是工作代码!

 public void OnClickButton(View paramView)
  {
    int btn_id = paramView.getId();

       if (btn_id == R.id.hint_field)
       {
     //set up dialog
           if (hint != null && hint.length()>0) {
          final Dialog dialog = new Dialog(Activity.this, R.style.Theme_New);
          dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
          dialog.setContentView(R.layout.custom_dialog);

          LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View main = inflater.inflate(R.layout.custom_dialog, null);
          dialog.setContentView(main);
          WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

          LinearLayout linear = (LinearLayout)main.findViewById(R.id.ad_layout);

              ad = new AdView(this, AdSize.IAB_MRECT, "a15xxxxxxxx");


        AdRequest request = new AdRequest();
        Set<String> keywords = new HashSet<String>();
        keywords.add("game");
        request.setKeywords(keywords);

          linear.addView(ad);
          ad.loadAd(request);

          dialog.setTitle("Title :");
          dialog.setCancelable(true);

           //set up text
          TextView text = (TextView) dialog.findViewById(R.id.hint_text);
          text.setText(hint);


         //set up button
           Button button = (Button) dialog.findViewById(R.id.Button01);
           button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
                   dialog.dismiss();
              }
          });

          // now that the dialog is set up, it's time to show it    
           lp.width = width;
           lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
           dialog.show();
           dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
           dialog.getWindow().setAttributes(lp);
           dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
           dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
     }
   }    
}

关于android - 自定义对话框上的 Admob 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13779629/

相关文章:

swift - Admob 不显示广告

java - 类未找到异常 : Didn't find class "com.google.android.gms.ads.AdView"

android - 错误:Could not initialize class org. gradle.tooling.internal.consumer.loader.DefaultToolingImplementationLoader

java - 接口(interface) java/Android 不能从子 fragment 到父 fragment

iphone - FBConnect (IPHONE),自定义发布对话框

android - 尝试显示 Facebook Android SDK 添加好友对话框时出错

android - Dagger 2 - 组件作用域编译错误

android - QT Windows 到 Android 必要性项目变更

android - 如何创建一个应该作为后续 Intent 的 Intent 以及可以在对话流中直接触发的主要 Intent ?

Android + AdMob 问题