Android横幅广告示例;未知套餐'R'

时间:2014-04-15 01:57:45

标签: java android package

因此,我正在努力让广告适用于我的第一个Android ap,我正在关注此处的Google开发人员教程:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals

因此,我的代码看起来像示例中的样子:

package com.google.example.gms.ads.banner;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

/**
 * A simple {@link Activity} that embeds an AdView.
 */
public class BannerSample extends Activity {
  /** The view to show the ad. */
  private AdView adView;

  /* Your ad unit id. Replace with your actual ad unit id. */
  private static final String AD_UNIT_ID = "TO_BE_DISCOVERED_SHORTLY";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
  }

  @Override
  public void onResume() {
    super.onResume();
    if (adView != null) {
      adView.resume();
    }
  }

  @Override
  public void onPause() {
    if (adView != null) {
      adView.pause();
    }
    super.onPause();
  }

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();
  }
}

然而,当我编译它时,我得到以下错误:

BannerSample.java:25: error: package R does not exist
    setContentView(R.layout.activity_main);
                    ^
BannerSample.java:34: error: package R does not exist
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
                                                       ^
2 errors

我怀疑我的答案的解决方案可能非常简单,但我不确定这些Rs的来源是什么,教程没有说明任何内容。帮助

我正在使用这个javac命令进行编译:

javac -cp ".;android.jar;google-play-services.jar" BannerSample.java

1 个答案:

答案 0 :(得分:0)

每次编译Android项目时(如果你至少都在使用eclipse),R包就会被删除然后重建。此错误非常常见,这意味着您的代码中出现了一个错误,导致构建过程停止,因此也会创建R包。

如果之前有效,请尝试确定它何时停止工作并分析导致它不编译的代码,以便找到错误。通常,错误位于XML布局文件中,导致无法构建应用程序

可能是您的问题是由不同的原因造成的,但在每种情况下,我都看到存在此问题,原因始终是编译错误。

快乐狩猎:)

编辑: 查看this答案,它可能只包含您需要的信息