I\'m trying to implement admob in my app, but the app failed to start after I added it.
This is the activity code: http://code.google.com/p/zhuang-dict/source/browse
Have You added this to Your Manifest?
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
Like shown in the tutorial by google:
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
The adUnitId has to be the Id for Your app, did You set this ID or did You really set "myid" like in your code shown above?
Here is some additional information, All seems to be that the AdMob Lib is not bundled to Your project. This could cause of many different reasons. Read the steps and get sure, You have really done this, if not, do it:
create a "libs" folder inside your project in Eclispe. BE SURE THAT THE FOLDER IS NAMED "libs" AND NOT "lib". THIS MISTAKE IS OFTEN DONE
copy the jar file from admob folder into Your project into the libs folder
put this into your Manifest.xml
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
Your Manifest.xml should look similar to this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:label="@string/app_name" android:name="BannerExample">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
NOTE: Put this line above under Your activity, not inside
set permissions inside the manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
create the xml layout for example like shown in the tutorial
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</LinearLayout>
Additionally, if the steps above did not work, You could do this inside Your Activity where You want to show the admob. Refer to Your admob xml id in onCreate() of Your Activity:
public class AdMobActivity extends Activity {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
// 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);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
In my App, I have done both, usually it is only necessary to do it in XML or Code, but with both it works fine too.
Try these steps:
libs
folder in your project.jar
file to the libs
folder.libs
folder.I had this problem after upgrading to the 15th or he 16th version of the ADT. I do not remember which.
Don't forget to remove the earlier reference to the library before doing the above.
Make sure you are initializing AdMob, otherwise you will get this error:
Fail to get isAdIdFakeForDebugLogging
MobileAds.initialize(this, AD_MOB_ID);
If you have added Google AdView from the palette and app stopped working.
Go to "Gradle Scripts" folder in Android Studio Find the build.gradle(Module: app)
Remove this line from the file end
implementation 'com.google.android.gms:play-services-ads:18.1.1'
Save the file and Build the Project again. Everything will work fine again
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-ads:18.1.1'
}