在 android 中使用 admob 完全自定义视图

发布于 2024-12-10 20:20:26 字数 706 浏览 2 评论 0原文

我在理解布局方面遇到了真正的问题。我正在尝试将 admob 放入我的应用程序中,但到目前为止我还没有理由使用布局。我会向你展示我的活动课上有什么。

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    View tfview = new TFView(this);
    setContentView(tfview);}

然后我继续用 Java 编写整个应用程序,而不使用任何 XML。如何将此视图和 Admob 放到布局上?我尝试通过这样做将它们都添加到布局中:

LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
    AdView adView = new AdView(this, AdSize.BANNER, pubID);
    layout.addView(adView);
    layout.addView(tfview);
    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request);   

但我收到某种空指针异常错误。有人可以帮忙吗?

提前致谢 -德里克

I am having real problems understanding Layouts. I am trying to put admob into my app, but up until this point i have had no reason to use a layout. I'll show you what I have in my activity class.

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    View tfview = new TFView(this);
    setContentView(tfview);}

I then proceed to have the entirety of my application written in Java, without the use of any XML. How to I put both this view and the Admob onto a layout? I tried adding both of them to the layout by doing this:

LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
    AdView adView = new AdView(this, AdSize.BANNER, pubID);
    layout.addView(adView);
    layout.addView(tfview);
    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request);   

but I receive some sort of null pointer exception errors. Can Anybody help?

Thanks in advance
-Derek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扎心 2024-12-17 20:20:26

感谢您的所有回复。我最终只是阅读了对类似问题的其他一些回答,并以编程方式构建了我的布局(如果这是一个词的话)。所以基本上我是这样做的:

 AdView adView = new AdView(this, AdSize.BANNER, pubID);

    FrameLayout layout = new FrameLayout(this);
    FrameLayout.LayoutParams gameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
            FrameLayout.LayoutParams.FILL_PARENT);
    FrameLayout.LayoutParams adsParams =new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 
            FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL); 
    layout.addView(tfview, gameParams);
    layout.addView(adView, adsParams);
    setContentView(layout);
    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request);     

它就像一个魅力。再次感谢大家。

-德里克

Thanks to all of your responses. I eventually just read some other responses to similar questions and built my layout on programatically(if thats a word). So basically I did this:

 AdView adView = new AdView(this, AdSize.BANNER, pubID);

    FrameLayout layout = new FrameLayout(this);
    FrameLayout.LayoutParams gameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
            FrameLayout.LayoutParams.FILL_PARENT);
    FrameLayout.LayoutParams adsParams =new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 
            FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL); 
    layout.addView(tfview, gameParams);
    layout.addView(adView, adsParams);
    setContentView(layout);
    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request);     

And it works like a charm. Thanks again to all of you.

-Derek

习ぎ惯性依靠 2024-12-17 20:20:26

确保 R.id.linearLayout 实际上是一个有效的 Id,与您要添加 Admob 内容的视图相对应。

如果你想更好地处理 XML 布局(我建议你这样做,因为它们确实是你应该在 android 中进行布局的方式),请查看 HelloViews 教程

Make sure that R.id.linearLayout is actually a valid Id that corresponds to the view to which you want to add your admob content.

If you want to get a better handle on XML Layouts (which I suggest you do, becuase they really are the way you should be doing layouts in android), checkout the HelloViews tutorials.

软的没边 2024-12-17 20:20:26

假设您的 xml 文件是 res/layout 文件夹中的 main.xml,然后将内容视图设置为您的 main.xml 文件。

    setContentView(R.layout.main);

设置 contentView 定义您从哪里提取资源。从您对 Kurtis 答案的评论来看,您的 xml 文件看起来不错,但是您必须将 xml 文件设置为 contentView 才能引用其布局。这就是为什么您找不到该视图,因此您的 layout 变量为 null,并最终导致异常。

Assuming your xml file is main.xml in the res/layout folder, then set the content view to your main.xml file.

    setContentView(R.layout.main);

Setting the contentView defines where you are pulling your resources from. Your xml file looks fine from your comment on Kurtis's answer, but you have to set the xml file as the contentView in order to be able to reference its layouts. That is why you can't find that view, so your layout variable is null, and it eventually causes the exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文