2

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/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java

The main.xml:

<?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"
    >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    >
        <ImageButton
            android:id="@+id/ts"
            android:contentDescription="@string/tts_contentDescription"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" 
            android:src="@android:drawable/ic_btn_speak_now"
        />
        <ImageButton
            android:id="@+id/search"
            android:contentDescription="@string/search_contentDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/ts"
            android:src="@android:drawable/ic_search_category_default"
        />
        <AutoCompleteTextView
            android:id="@+id/edit"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:focusable="true"
            android:singleLine="true"
            android:layout_toLeftOf="@id/search"
            android:hint="@string/edit_hint"
        />
    </RelativeLayout>
    <WebView
        android:id="@+id/result"
        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="myId"
                     ads:adSize="BANNER"
                     ads:loadAdOnCreate="true"
     />
</LinearLayout>
user2190227
  • 23
  • 1
  • 7

4 Answers4

4

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?

EDIT

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:

Update Eclipse and Android SDK:

  1. open Eclipse as Administrator
  2. go to Help-->Check for updates and run the updates
  3. set everything checked thats shown in the next window. It is not necessary but it would not be amiss to update all
  4. accept the terms and licensees and press finish...the updates will start and needs some time
  5. restart Eclipse as Admin if updates finished. It is possible that You get an error message that not all updates could be done...go forward

Update Android SDK:

  1. in Eclipse goto Window-->Android SDK Manager
  2. at the Column "Status" You see if a update is available or not. Select all updates...
  3. if finished, restart Eclipse again

Work With Admob - put jar file and set path:

  1. download AdMob SDK from Google site(could be a different link depends on country) https://developers.google.com/mobile-ads-sdk/download?hl=de
  2. unzip the file into a directory (no matter where)
  3. 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


  4. copy the jar file from admob folder into Your project into the libs folder

  5. click right mouse button on jar file inside lib folder and choose Build Path-->add to build path
  6. To get sure everything is really done, click right mouse button on Your project and choose properties
  7. select Java-Build-Path and go to Libraries Tab
  8. select add external jar and select this jar from your admob folder
  9. select ok

get started

  1. 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

  1. set permissions inside the manifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
  2. 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>
    
  3. 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.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Added but the app still fail to start. – user2190227 Mar 21 '13 at 15:17
  • Yes, I can it to my own ID. I actually following the guide from http://www.wikihow.com/Add-Advertisements-to-Your-Android-Applications-Using-Admob – user2190227 Mar 21 '13 at 15:27
  • maybe there is something wrong with Your Path...try to follow the answer from Siddarth Lele and give response if it worked – Opiatefuchs Mar 21 '13 at 15:40
  • I tried Siddarth Lela's method. Although I'm still failed to get it to work, I get a shorter logcat. Please have a look at comment below. – user2190227 Mar 21 '13 at 15:51
  • ok...I think its a good way to start from the beginning. Please post Your complete xml where the google.admob is inside, even please post Your Activity where You want to show this xml. – Opiatefuchs Mar 22 '13 at 08:42
  • OK, Id edited Your ID, it´s better that nobody see this....but for now, what is irritating me is the webView. You set the WebView to fill_parent the height, this is outside the relative layout and will fill the whole place. Could You try to remove this webView and try to run Your activity again. If You use this webview inside Your code, don´t forget to comment it out to get no error... – Opiatefuchs Mar 22 '13 at 10:00
  • I remove webview and also remove searchResultWebView = (WebView) findViewById(R.id.result); from the code. But the app still crash. – user2190227 Mar 22 '13 at 11:23
  • man, thats all hard, in between I put some new tips inside my Answer. Please look at this, and be very very sure You have really all done this. If You are not sure, try again all the steps. If this error then still resists, wait and I will try Your xml this weekend and try to find the mistake. – Opiatefuchs Mar 22 '13 at 11:53
  • Thanks for such a detailed answer. I indeed following all the instruction. Here is the screenshot: http://i6.photobucket.com/albums/y248/zfc/Screenshot3_zps855a73ee.png I don't understand your third step as I'm new in Android development. Can you tell me I should add that under which line? – user2190227 Mar 22 '13 at 14:11
  • Your Screenshot looks good. The order is not important (sometimes it is, but not here). What did You mean with third step? Which one? I edited again, how the manifest.xml should look like and how the Activity calls the AdView. Now, start at the place you made the screenshot and select on the left side "Android", scroll down the page till the end. There must be a separated box, here You should see the lib You added. Make a screenshot and I will look.... – Opiatefuchs Mar 22 '13 at 15:21
  • I mean your java code, public class AdMobActivity. I'm not sure how to do it. Should create a new java file or paste it inside ZhuangDictActivity.java. I did both, but get error. I selected the "Android" but unexpectedly, there is no lib added http://i6.photobucket.com/albums/y248/zfc/Screenshot4_zps81efb68e.png What I went wrong actually? – user2190227 Mar 23 '13 at 01:19
  • No I expressed it wrong, the pic You send me was ok. There has to be no lib inside, this is only if You want to import from an existing project. So thats ok. You have to refer to the adview inside Your activiy that will show the admob. If you got main.xml specified with the adview, declare the adview inside the activity where the main.xml depends. Do it inside the onCreate() Method. – Opiatefuchs Mar 23 '13 at 10:35
  • there is no pm system in stackoverflow? I'm really frustrated to get this work. As I know nothing in programming, I can't understand what you mean although to the fact the your answer is so detailed. I don't know whether this is against the rule. Can I send my project to you and you help me implement admob? I'm willing to pay you money via PayPal. – user2190227 Mar 23 '13 at 11:18
  • yes I try, but don´t know if I could help You. Maybe it needs a little bit time....no money..we are here to help each other :) opiatefuchs@googlemail.com – Opiatefuchs Mar 23 '13 at 11:29
  • Sent to your email. Thank you so much. =) – user2190227 Mar 23 '13 at 12:14
  • Problem solved! Please check your email. Thank you very much! – user2190227 Mar 24 '13 at 08:36
1

Make sure you are initializing AdMob, otherwise you will get this error:

Fail to get isAdIdFakeForDebugLogging

MobileAds.initialize(this, AD_MOB_ID);
live-love
  • 48,840
  • 22
  • 240
  • 204
0

Try these steps:

  1. Create a libs folder in your project.
  2. Add the Google AdMobs jar file to the libs folder.
  3. Import the AdMob library from the libs folder.
  4. Confirm that the new method of adding the libray works by: Right Click Project -> Build Path -> Configure build Path -> switch to the Libraries Tab and confirm the Google AdMobs library is present in the list.

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.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • @user2190227: Sorry for the late reply. Can you post your Activity code? I am not sure this error is related to the AdMob thingy. – Siddharth Lele Mar 22 '13 at 04:09
  • My activity code is basically based on this source http://code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java – user2190227 Mar 22 '13 at 07:40
0

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'
}
Vinay Verma
  • 877
  • 8
  • 15