问题
I finished developing a game for Android using a slightly modified version of the framework at http://www.kilobolt.com/unit-4-android-game-development.html. Then I followed this tutorial:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start and successfully added an ad but not on my main activity as this framework does not have a .xml file for the main activity (surface view in pure java code). However, I want ads to appear on the main activity too since that contains 95% of the game. I managed to successfully add a test view (button) on the surfaceview using an online tutorial but when I changed the button to an AdView I got this error:
"AdRequest.Builder cannot be resolved to a type" in
AdRequest adRequest = new AdRequest.Builder().build();
.
Can someone tell me what could cause this error? Eclipse accepted the exact same code in my other activity on the same project but not here. I have made the necessary imports.
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);
}
Any help is appreciated.
回答1:
Use this import com.google.android.gms.ads.AdRequest;
回答2:
Use This Library
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdRequest;
来源:https://stackoverflow.com/questions/29864713/adrequest-builder-cannot-be-resolved-to-a-type