在 android 中使用 admob 完全自定义视图
我在理解布局方面遇到了真正的问题。我正在尝试将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的所有回复。我最终只是阅读了对类似问题的其他一些回答,并以编程方式构建了我的布局(如果这是一个词的话)。所以基本上我是这样做的:
它就像一个魅力。再次感谢大家。
-德里克
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:
And it works like a charm. Thanks again to all of you.
-Derek
确保 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.
假设您的 xml 文件是
res/layout
文件夹中的main.xml
,然后将内容视图设置为您的main.xml
文件。设置 contentView 定义您从哪里提取资源。从您对 Kurtis 答案的评论来看,您的 xml 文件看起来不错,但是您必须将 xml 文件设置为 contentView 才能引用其布局。这就是为什么您找不到该视图,因此您的
layout
变量为 null,并最终导致异常。Assuming your xml file is
main.xml
in theres/layout
folder, then set the content view to yourmain.xml
file.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.