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

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. Help?

I am compiling using this javac command:

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. 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.

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

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归因
不隶属于 StackOverflow
scroll top