Android:Google Admob 示例存在问题

发布于 2024-11-08 15:46:53 字数 1690 浏览 0 评论 0原文

我正在尝试创建一个仅包含基本要素的示例 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 技术交流群。

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

发布评论

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

评论(2

氛圍 2024-11-15 15:46:53

发生 NPE 是因为 layoutnull,即不存在 ID 为 R.layout.main 的视图。

解释原始示例时,

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

您需要将属性 android:id 添加到您的 LinearLayout (ID 应与 R.id.main 不同)。

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

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

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

鱼窥荷 2024-11-15 15:46:53

修复 NPE 错误后,您可能会收到进一步的错误。

在将布局添加到视图之前,您必须设置布局的高度和宽度。

AdView adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
adView .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutMain);
layout.addView(adView);

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.

AdView adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
adView .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutMain);
layout.addView(adView);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文