我使用http://www.kilobolt.com/unit-4-android-game-development.html框架的略微修改版本完成了针对Android的游戏开发。然后我按照本教程:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start成功添加了一个广告,但没有成功添加到我的主要活动中,因为此框架没有主活动的.xml文件(纯Java代码中的表面视图)。但是,我希望广告也出现在主要活动中,因为它包含95%的游戏。我设法使用在线教程在surfaceview上成功添加测试视图(按钮),但当我将按钮更改为AdView时出现此错误:
“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);
}
感谢任何帮助。
答案 0 :(得分:2)
使用此import com.google.android.gms.ads.AdRequest;
答案 1 :(得分:0)
使用此库
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdRequest;