简体   繁体   English

Android横幅广告示例; 未知包装“ R”

[英]Android Banner Ads Example; Unknown Package 'R'

So I am trying to get ads to work for my first Android ap, I am following the Google developers tutorial found here: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals 因此,我正在尝试使广告能够在我的第一个Android应用程序上正常工作,我正在关注以下位置的Google开发人员教程: https//developers.google.com/mobile-ads-sdk/docs/admob/fundamentals

Thus my code looks like what it looks like in the example: 因此,我的代码看起来像示例中的样子:

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

However, when I compile this I get the following errors: 但是,当我对此进行编译时,出现以下错误:

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

I suspect the solution to my answers may be incredibly simple, but I'm not sure where these Rs are suppose to come from and the tutorial doesn't say anything about it. 我怀疑答案的解决方案可能非常简单,但是我不确定这些Rs应该来自何处,本教程对此也没有说明。 Help? 救命?

I am compiling using this javac command: 我正在使用以下javac命令进行编译:

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

Every time you compile your Android project (if you are working on eclipse at least) the R package gets deleted and then gets rebuild. 每次编译Android项目时(如果您至少在eclipse上工作),R包都会被删除,然后重新构建。 This error is pretty common and it means that you have an error in your code that stopped the building process and so also the creation of the R package. 此错误非常普遍,这意味着您的代码中存在错误,该错误停止了构建过程,因此也停止了R包的创建。

If it worked before then try identifying at which time did it stop working and analyze the code that caused it not to compile so to find the error. 如果它之前可以工作,则尝试确定它在什么时候停止工作,并分析导致其无法编译的代码,以便查找错误。 Normally the error is in the XML layout file that prevented the application from being built 通常,错误是在XML布局文件中导致无法构建应用程序

It may be that your problem is caused by a different reason, but in every case that that I've seen that this problem is present, the cause is always a compilation error. 可能是您的问题是由不同的原因引起的,但是在我发现存在此问题的每种情况下,原因始终是编译错误。

Happy hunting :) 狩猎快乐:)

edit: Check out this answer, it may contain just the info you need 编辑:签出这个答案,它可能只包含您需要的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM