[英]Android Banner Ads Example; Unknown Package 'R'
因此,我正在嘗試使廣告能夠在我的第一個Android應用程序上正常工作,我正在關注以下位置的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
每次編譯Android項目時(如果您至少在eclipse上工作),R包都會被刪除,然后重新構建。 此錯誤非常普遍,這意味着您的代碼中存在錯誤,該錯誤停止了構建過程,因此也停止了R包的創建。
如果它之前可以工作,則嘗試確定它在什么時候停止工作,並分析導致其無法編譯的代碼,以便查找錯誤。 通常,錯誤是在XML布局文件中導致無法構建應用程序
可能是您的問題是由不同的原因引起的,但是在我發現存在此問題的每種情況下,原因始終是編譯錯誤。
狩獵快樂:)
編輯:簽出這個答案,它可能只包含您需要的信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.