我完成了Android的遊戲開發,使用框架的稍微修改版本http://www.kilobolt.com/unit-4-android-game-development.html。然後我遵循本教程:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start併成功添加了廣告,但未添加到我的主要活動中,因爲此框架沒有用於主要活動(純Java代碼中的表面視圖)的.xml文件。不過,我還希望廣告也出現在主要活動上,因爲它包含95%的遊戲。我設法使用一個在線教程,成功添加的surfaceview測試視圖(按鈕),但是當我改變了按鈕的AdView中我得到這個錯誤:AdRequest Builder無法解析爲
在
「AdRequest.Builder不能被解析爲一個類型」AdRequest adRequest = new AdRequest.Builder().build();
。
有人能告訴我什麼可能會導致此錯誤? Eclipse在同一個項目中的其他活動中接受完全相同的代碼,但不在此處。我已經做了必要的進口。
import com.google.ads.AdRequest;
import com.google.android.gms.ads.AdView;
。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appContext = this.getApplicationContext();
mainLayout = new FrameLayout(this);
adLayout = new RelativeLayout(this);
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // google test id
//AdRequest adRequest = new AdRequest.Builder().build();
adView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams lp1 = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
adLayout.setLayoutParams(lp2);
adLayout.addView(adView);
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adView.setLayoutParams(lp1);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
int frameBufferWidth = isPortrait ? 800 : 1280;
int frameBufferHeight = isPortrait ? 1280 : 800;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float scaleX = (float) frameBufferWidth/metrics.widthPixels;
float scaleY = (float) frameBufferHeight/metrics.heightPixels;
GameScreen.deviceWidth = metrics.widthPixels;
GameScreen.deviceHeight = metrics.heightPixels;
prefs = this.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
prefsExist = prefs.contains("showValueAddition");
loadDefaultSettings();
renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(this);
audio = new AndroidAudio(this);
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = getInitScreen(prefs);
mainLayout.addView(renderView);
mainLayout.addView(adLayout);
setContentView(mainLayout);
}
任何幫助表示讚賞。
可以張貼代碼爵士的那部分? – Elltz
@Elltz更新的第一篇文章 – Steyiak