安卓:问题与谷歌Admob的样本 [英] Android: Problem with Google Admob sample

查看:327
本文介绍了安卓:问题与谷歌Admob的样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建只是最基本的一个示例应用程序的AdMob。我跟着指示,甚至复制大部分来自这个网站的code:的 HTTP://$c$c.google.com/mobile/ads/docs/android/fundamentals.html ,但应用程序保持力关我的VD和我的真实设备上。调试器提供了一个RuntimeException的在该行:layout.addView(AD浏览报)。我敢肯定,有一个简单的解决办法,但我不能弄明白。我搜索周围,但大多数网上信息是pre-谷歌AdMob的 - 它使用了不同的方法。

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.

下面是我的MainActivity(我唯一的活动):

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

这是我的错误的logcat输出:

Heres my logcat error output:

05-22 17:49:24.534:ERROR / AndroidRuntime(19619):产生的原因:显示java.lang.NullPointerException

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: ERROR/AndroidRuntime(19619): at com.example.admobsample.MainActivity.onCreate(MainActivity.java:22)

05-22 17:49:24.534:ERROR / AndroidRuntime(19619):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

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):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)

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多个

05-22 17:49:24.534: ERROR/AndroidRuntime(19619): ... 11 more

05-22 17:49:26.024:ERROR / PackageInstallationReceiver(17825):删除/data/local/tmp/com.example.admobsample.apk失败

05-22 17:49:26.024: ERROR/PackageInstallationReceiver(17825): Remove /data/local/tmp/com.example.admobsample.apk Fail!

推荐答案

在NPE是因为布局 ,也就是说,没有与ID没有意见 R.layout.main

The NPE happens because layout is null, i.e., there is no view with the ID R.layout.main.

国米preting最初的例子

Interpreting the original example

    // Lookup your LinearLayout assuming it’s been given
    // the attribute android:id="@+id/mainLayout"
    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

您需要的属性机器人添加:ID 您的LinearLayout(ID应该是从 R.id.main )。

you need to add the attribute android:id to your LinearLayout (the ID should be different from R.id.main).

这篇关于安卓:问题与谷歌Admob的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆