Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app fail to start after adding admob

Tags:

android

admob

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>
like image 385
user2190227 Avatar asked May 08 '26 15:05

user2190227


1 Answers

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.

like image 200
Opiatefuchs Avatar answered May 11 '26 05:05

Opiatefuchs