Admob - Problems with Ads in APP

Asked

Viewed 965 times

0

I did everything as ADMOB site tutorial says, I put the layout in XML, imported the path from Google Play Sevices and added the tutorial code lines, my application compiles, but no longer opens on smartphone, an alerat message appears saying that the app has stopped and in the Eclipse log appears the following message (below), I need a help.

Erro ao executar o app

Message in the Eclipse:

05-26 10:47:02.007: E/AndroidRuntime(9092): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.CalculoHE.namespace/your.CalculoHE.namespace.CalculoHoraExtraActivity}: java.lang.NullPointerException

Class:

package your.CalculoHE.namespace;


import org.apache.cordova.DroidGap;
import com.google.android.gms.ads.*;


import android.os.Bundle;

public class CalculoHoraExtraActivity extends DroidGap {    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        super.setIntegerProperty("splashscreen", R.drawable.sobreaviso);
        super.loadUrl("file:///android_asset/www/index.html", 3000);

        // Consultar o AdView como um recurso e carregar uma solicitação.
    AdView adView = (AdView)this.findViewById(R.id.adView);
    adView.loadAd(new AdRequest());

    }    
   }     

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/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="ca-app-pub-6560993155721972/3382486845"
                         ads:adSize="BANNER"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hora extras"
    />
</LinearLayout>

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.CalculoHE.namespace"
    android:versionCode="4"
    android:versionName="1.2" >

    <uses-sdk android:minSdkVersion="9" />

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
        />

    <application android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

       <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>


        <activity android:configChanges="orientation|keyboardHidden" android:name=".CalculoHoraExtraActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>


    </application>
            <uses-permission android:name="android.permission.INTERNET"/>
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>
  • No Math, I’m using the Intel appframework

  • I don’t understand the subject, sorry, I’ll pass the ball :)

  • 1

    Could you inform the Nullpointer line? It’s the line: adView.loadAd(adRequest);?

  • @Wakim, is this line even! adView.loadAd(new Adrequest());

  • @Wakim appears this error message with the constructor The constructor Adrequest() is Undefined, I don’t know how to solve this!

  • Using ANT or Gradle to build the app? This type of message occurs when the class is not in the classpath.

  • @Pauloroberto does not use the default constructor of AdRequest, it is private. Use the Builder...

  • @Wakim I’m using the ANT yes, man I’m already floating in it there, I’m almost giving up... If not following the tutorial of the site the right deal.

  • Looking at your code, it’s the same as mine, the problem is in relation to Layout, because it seems that it is not finding Adsview (using findViewById). The problem is in the layout, have to see if Droidgap is not preventing your layout, since you load an html afterwards right...

Show 4 more comments

1 answer

2


I looked at the Droidgap code in the Cordova. And from what I understand, it is not possible to do the way you did, including an android layout of yours with Adsview, because it always overwrites the layout.

You’ll have to go that way:

import android.os.Bundle; 
import org.apache.cordova.*;
import com.google.ads.*;
import android.widget.LinearLayout;

public class MainActivity extends DroidGap {
    private static final String AdMob_Ad_Unit = "xxxxxxxxxxxxxxx";
    private AdView adView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")

        adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit); 
        LinearLayout layout = super.root; 
        layout.addView(adView); 
        AdRequest request = new AdRequest();

        // Comment this out before publishing.
        // request.addTestDevice(AdRequest.TEST_EMULATOR);
        adView.loadAd(request); 
    }
}

Another suggestion is, although the tutorial uses the DroidGap, use the CordovaActivity for the DroidGap this marked as @Deprecated.

There is a tutorial on codetheory, that will help you include Ads in your app.

  • Thank you so much for your help!

  • If successful, mark as correct :P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.