Android:Google Admob 示例存在问题
我正在尝试创建一个仅包含基本要素的示例 admob 应用程序。我按照说明进行操作,甚至从该网站复制了大部分代码:http: //code.google.com/mobile/ads/docs/android/fundamentals.html,但该应用在我的 VD 和真实设备上始终强制关闭。调试器在以下行给出“RuntimeException”:layout.addView(adview)。我确信有一个简单的解决方案,但我无法弄清楚。我到处搜索过,但网上的大部分信息都是针对谷歌之前的 admob - 它使用了不同的程序。
这是我的 MainActivity (我唯一的活动):
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
layout.addView(adView); //RuntimeException at this line
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
这是我的 logcat 错误输出:
05-22 17:49:24.534: ERROR/AndroidRuntime(19619): Caused by: java.lang.NullPointerException
05-22 17:49:24.534: ERROR /AndroidRuntime(19619): 在 com.example.admobsample.MainActivity.onCreate(MainActivity.java:22)
05-22 17:49:24.534: 错误/AndroidRuntime(19619): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation. java:1047)
05-22 17:49:24.534: 错误/AndroidRuntime(19619): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-22 17:49:24.534: 错误/AndroidRuntime(19619) : ... 11 更多
05-22 17:49:26.024: 错误/PackageInstallationReceiver(17825): 删除 /data/local/tmp/com.example.admobsample.apk 失败!
I'm trying to create a sample admob application with just the bare essentials. I followed the directions and even copied most of the code from this site: http://code.google.com/mobile/ads/docs/android/fundamentals.html, but the app keeps force closing on my VD and on my real device. The debugger gives a "RuntimeException" at the line: layout.addView(adview). I'm sure there is a simple solution to this, but I can't figure it out. I've searched around, but most of the information online is for pre-google admob - which used a different procedure.
Here is my MainActivity (my only Activity):
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
layout.addView(adView); //RuntimeException at this line
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
Heres my logcat error output:
05-22 17:49:24.534: ERROR/AndroidRuntime(19619): Caused by: java.lang.NullPointerException
05-22 17:49:24.534: ERROR/AndroidRuntime(19619): at com.example.admobsample.MainActivity.onCreate(MainActivity.java:22)
05-22 17:49:24.534: ERROR/AndroidRuntime(19619): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-22 17:49:24.534: ERROR/AndroidRuntime(19619): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-22 17:49:24.534: ERROR/AndroidRuntime(19619): ... 11 more
05-22 17:49:26.024: ERROR/PackageInstallationReceiver(17825): Remove /data/local/tmp/com.example.admobsample.apk Fail!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生 NPE 是因为
layout
为null
,即不存在 ID 为R.layout.main
的视图。解释原始示例时,
您需要将属性
android:id
添加到您的 LinearLayout (ID 应与R.id.main
不同)。The NPE happens because
layout
isnull
, i.e., there is no view with the IDR.layout.main
.Interpreting the original example
you need to add the attribute
android:id
to your LinearLayout (the ID should be different fromR.id.main
).修复 NPE 错误后,您可能会收到进一步的错误。
在将布局添加到视图之前,您必须设置布局的高度和宽度。
Once you fix your NPE error you may get a further error.
You have to set a layouts height and width before adding it to a view.