I would like to get some help with integrating AdMob into a game structured like Frozen Bubble. I have made the layout with the adview in itself, and tried to load ad in the FrozenBubble.java (which file is the appropriate for it in my game), but it is crashing with NullPointerException for some reason.
Here is my FrozenBubble like code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isRunning = true;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
requestWindowFeature(Window.FEATURE_NO_TITLE);
restoreGamePrefs();
AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
adview.loadAd(re);
...
It is the Admob based code... But it is crashing with NullPointerException.
In the layout I have the GameView and the AdMob View:
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_CODE_HERE"
ads:adSize="BANNER"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
I don't if it is enough information, but not, then you can find the frozen bubble source here, which my game based on:
Frozen Bubble Google Code
Please look at it, and help me if you can!
Thanks in advance!
You never called setContentView with your layout file. As a result, AdView adview = (AdView)findViewById(R.id.adView); will return null, and adview.loadAd(re); will crash.
Attach your layout file to your activity prior to calling findViewById.
Related
my admob ads works perfectly (mediation by admob, running like 7 activities where i have these ads) but when i started campaign in one of my activity with one simple AD (320x50) it doesnt show. Tried every type of campaign noone worked.
Is there anything what i need to do? Something in code? or in Admob settings (except cpm, chose which activity, type of AD etc... already done that). I was searching for this problem the whole week but didnt find any succes.
If someone could help me it will be awesome because there is no support on AdMob except of FAQ which tells you nothing.
There is a sample of code activity and layout:
<LinearLayout
android:id="#+id/tejbl_tlacitka"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:orientation="vertical"
>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
style="#style/bannerpozadi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-4412963312739253/5142509411" />
</LinearLayout>
Activity:
public class oAplikaciActivity extends AppCompatActivity {
private AdView mAdview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oaplikaci);
setTitle("O aplikaci");
mAdview = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdview.loadAd(adRequest);
I recently migrated my project from AdMob SDK to Google service Ads, however I got a tiny problem with the Google service Ads.
I have a banner ad view in the activity, and I would dynamically adjust the buttons in the layout depending on whether or not an ad is loaded. It worked fine when I used AdMob SDK, but now with Google service Ads, the banner is always reserved there with blank before the ad is loaded. And if the ad cannot be loaded (say without network), the blank view is there, which is rather ugly! This is also why I would like to adjust the layout dynamically...
Did I missed anything while I changed the code? Thank you for help!
Here is an excerpt of the java code and layout file:
Java:
import com.google.android.gms.ads.*;
...
public class MyActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
...
AdView adView = (AdView)findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Layout XML:
...
<com.google.android.gms.ads.AdView
android:id="#+id/ad"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="AD_PUB_ID"
ads:adSize="BANNER"/>
</RelativeLayout>
You can add an AdListener, and make the ad visible only when it receives an ad.
The code looks like this:
final AdView ad = new AdView(context);
ad.setAdUnitId(publisherId);
ad.setAdSize(AdSize.SMART_BANNER);
final AdListener listener = new AdListener() {
#Override
public void onAdLoaded() {
ad.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
};
ad.setAdListener(listener);
ad.setVisibility(View.GONE);
adParent.addView(ad);
ad.loadAd(new AdRequest.Builder().build());
Since there is/was an issue, that an AdMob ad generates an ANR, when it is destroyed when still loading. I normally also check on the visibility of the ad, before calling pause() or destroy().
Be careful because this is against Google Ad policy to have the ad banner pops up like that.
I received a mail from Google a month ago and had to change that behavior to avoid my app to be banned from the Play Store.
A lot of developers are receiving the same mail.
I am trying to add the new AdMob ads to my first application, I managed to arrange the code in the correct way but I have to problems:
First problem is my ad ID ca-app-pub-61413779example is getting multiple errors like: ca cannot be resolved to a variable, ba cannot be resolved to a variable, the literal
61413779 of type int is out of range.
The second problem is R.id.mainLayout, which is mainLayout, I don't get it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
// Create the adView
adView = new AdView(this, AdSize.BANNER, ca-app-pub-61413779example);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(adView);
adView.loadAd(new AdRequest());
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
I'm not familiar with your admob adding way, but I got another great way to add admob to your app using xml only.
first in your manifest add these permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
* the second is most important
then add this in your manifest inside the application tag
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
now in every activity you want the ad to appear on you add this in the layout.xml of the activity
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxxx"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_gravity="center"
android:layout_margin="10dp">
</com.google.ads.AdView>
and inside the top parent layout ex. linearlayout you add these under xmlns:android="xxx
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
now the ads should work perfectly :D
UPDATE: don't forget to download and place the GoogleAdMobAdsSdk-6.4.1.jar under the libs folder.
The AdView constructor expects a String as the third argument, so you're basically missing double quotes:
adView = new AdView(this, AdSize.BANNER, "ca-app-pub-61413779example");
As for your XML, ensure that you have the right ID. If only mainLayout is underlined, it is not the correct ID. If R is underlined, then it means your XML has an error somewhere and R.java isn't being generated.
since you set your content with activity_basic_screen.xml, you have to go to that xml.
you need to show that code for activity_basic_screen.xml and tell where you want to put the ad for specific details
for general, in your activity_basic_screen.xml, you have to have a LinearLayout or other layouts where you want to show/inflate the ad. for the one in your screenshot problem/Linearlayout, you have to have this one:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLayout"
>
you're getting that error because i think you don't have a linear layout with
android:id="#+id/mainLayout"
you can name it differently
android:id="#+id/givenName"
but you have to changed your reference in java of findViewById to "R.id.givenName"
for me, i usually use Ahmed's suggestion so i won't need to program it in java but i think programming in java is more dynamic.
hope that helps.
PS.
in your screenshot you cover your app name/project but you showed your package name. it's like the same thing because you can find your app in google play by using that.
This maybe a different answer to what you are looking for, but I would highly recommend using SMART_BANNER Over BANNER. look here at The table that list the standard banner sizes:
https://developers.google.com/admob/android/banner
Banner: size 320x50 only
Smart Banner: size change to Screen width x 32|50|90
Here is an example on how to do it:
-Layout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_r"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.adexample">
<RelativeLayout
android:id="#+id/adView_test"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
-Activity:
public class MainActivity extends AppCompatActivity {
private AdView adView;
private AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
admobCall();
}
// Function to Set Ads for Main Layout
private void admobCall(){
// Set the RelativeLayout id to the view
View adContainer = findViewById(R.id.adView_test);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
//Real Admob Main Activity Banner
adView.setAdUnitId(getString(R"YOUR_ADMOB_AD_ID"));
// Test google ID for Banner
// adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
((RelativeLayout)adContainer).addView(adView);
// This Request will bulid the ADs as a Test
//adRequest = new AdRequest.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// This Request Will bulid the ADs as Real
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
Also be sure to add "google()" to the project-level build.gradle, and
this implementation to dependencies
implementation 'com.google.android.gms:play-services-ads:17.2.1'
Check the Implementation instructions site just to be sure you are not missing something:
https://developers.google.com/admob/android/quick-start
Hope this was helpful :)
I am currently able to get the admob ads on ui view (in separate test project ), but i want to display this ad in GLSurfaceView .
I tried to load the ad in onCreate( ) method of activity and in present method of my screen (where all rendering is done) i called
MyGameActivity.mAdView.bringToFront(); //thought it will bring the ad in front of all the game objects.
now on running the project i can see the message in logcat window Recieved ad url "big url"
but i cant see the ad on screen. In my game there is only one activity and many game screens. please help me figure out how to display ads on my game screen.
You should modify your layout to be something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:tag="trueLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</RelativeLayout>
</LinearLayout>
This is my code, which is self explanatory.:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layoutMain = (LinearLayout) findViewById(R.id.layoutMain);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
adView = new AdView(this, AdSize.BANNER, "YourPersonalID#");
layoutMain.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout1.setOnTouchListener(this);
mTestHarness = new GLSurfaceView(this);
mTestHarness.setEGLConfigChooser(false);
mTestHarness.setRenderer(this);
mTestHarness.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
layout1.addView(mTestHarness);
}
After you get this right you will get the equivalent to the BannerEssentials tutorial app from the google play tutorials, but using GLSurfaceView instead.
I did some updates to my application today but now admob dont work right and it gives me an error in the ad saying adview missing xml attribute "adsize". now the changes i made have nothing to do with the ads and they have been the same since I implemented them and worked
this is my xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res/com.notify.me.lite"
android:layout_width="fill_parent"
android:id="#+id/mainLayout1"
android:layout_height="fill_parent" android:orientation="vertical">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="id"
ads:adSize="BANNER"/>
<WebView android:layout_width="fill_parent" android:id="#+id/webView1" android:layout_height="fill_parent"></WebView>
</LinearLayout>
and where its implemented
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_webview);
WebView browser = (WebView) findViewById(R.id.webView1);
browser.loadUrl("file:///android_asset/about.html");
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
as you can see adsize is there in the xml file so there should be no reason why I should be getting that error. Ideas as to why this is happening all of the sudden?
Google updated the AdMob library but did not yet document the required changes, as per this forum thread
The substance of the change is to update the ads namespace:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
No idea why its suddenly stoped working but whenever i use Admob i always put it in a seperate layout, with xml like:
<RelativeLayout android:id="#+id/rLayAdMob"
android:layout_width="match_parent" android:layout_height="50dip">
</RelativeLayout>
Then in the onCreate function i setup the adView like:
adView = new AdView(Menu.this, AdSize.BANNER, MY_AD_UNIT_ID);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rLayAdMob);
layout.addView(adView);
I've never had any issues doing it this way, and would only require a small change to your code.
Hope this helps,
Kenny