Problem when putting admob in android studio

1

I have created an application in android studio and it works fine but when you put the ad theme with admob it can not even be opened and it comes out all the time the application has stopped working. I have followed guides, tutorials, etc.

I have created a new project in android studio with the admob activity selecting ad in banner to test without entering any code if it works and the same error comes out, here I leave the code:

ALL UPDATED

AndroidManifest.xml                    

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> <!-- Include the AdActivity configChanges and theme. -->
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

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

MainActivity.java     package com.example.ruben.safewin;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editText4, editText5, editText6;
Button button;
TextView textView4;
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, getResources().getString(R.string.banner_ad_unit_id));

    // Load an ad into the AdMob banner view.
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            //Use your own Test Device Id, from LogCat :  Use AdRequest.Builder.addTestDevice("xxxxxxxxxxxx") to get test ads on this device.
            .addTestDevice("821F6B84B97F82FBEBE855AD542340A4")
            .build();
    mAdView.loadAd(adRequest);

    // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
    //Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
    editText4 = (EditText) findViewById(R.id.editText4);
    editText5 = (EditText) findViewById(R.id.editText5);
    editText6 = (EditText) findViewById(R.id.editText6);
    textView4 = (TextView) findViewById(R.id.textView4);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}


public void onClick(View v) {
    float num1, num2, porcentaje = 0;
    float stake, stake1, stake2 = 0;
    AlertDialog.Builder dialogo = new AlertDialog.Builder(this);
    dialogo.setMessage("Los campos no pueden estar vacíos");
    dialogo.setTitle("Error");
    dialogo.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    AlertDialog dialog=dialogo.create();

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(button.getWindowToken(), 0);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

strings.xml

<resources>
<string name="app_name">SafeWin</string>
<string name="action_settings">Settings</string>
<!-- -
    This is an ad unit ID for a banner test ad. Replace with your own banner ad unit id.
    For more information, see https://support.google.com/admob/answer/3052638
<!- -->
<string name="banner_ad_unit_id">ca-app-pub-3748175831230810/7065359892</string>

This is all, I do not know if maybe something is missing when copying and pasting, I hope an answer, thank you.

Here I leave the logcat:

2018-10-20 11: 24: 59.561 1510-2134 /? E / ANDR-PERF-JNI: com_qualcomm_qtiperformance_native_perf_io_prefetch_start

2018-10-20 11: 24: 59.562 563-3291 /? E / msm8916_platform: platform_check_backends_match: Invalid snd_device =

2018-10-20 11: 24: 59.562 563-3291 /? E / audio_hw_primary: start_dsm_feedback_process: pcm device id 43

2018-10-20 11: 24: 59.564 1510-2134 /? E / ActivityTrigger: activityStartTrigger: not whiteListedcom.example.ruben.safewin / com.example.ruben.safewin.MainActivity / 1

2018-10-20 11: 24: 59.566 1510-2134 /? E / ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.ruben.safewin / com.example.ruben.safewin.MainActivity / 1

2018-10-20 11: 24: 59.571 1510-2134 /? E / ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.ruben.safewin / com.example.ruben.safewin.MainActivity / 1

2018-10-20 11: 24: 59.581 563-3291 /? E / msm8916_platform: platform_get_snd_device_backend_index: no hw_interface set for device speaker

2018-10-20 11: 24: 59.581 563-3291 /? E / audio_hw_utils: send_app_type_cfg_for_device: Could not get the backend index for snd device speaker ret = -22

2018-10-20 11: 25: 00.649 27391-27419 / com.example.ruben.safewin E / cr_VariationsUtils: Failed reading seed file "/data/user/0/com.example.ruben.safewin/app_webview/ variations_seed_new ": /data/user/0/com.example.ruben.safewin/app_webview/variations_seed_new (No such file or directory)

2018-10-20 11: 25: 00.690 27423-27423 /? E // system / bin / webview_zygote32: Failed to make and chown / acct / uid_99524: Permission denied

2018-10-20 11: 25: 00.690 27423-27423 /? E / Zygote: createProcessGroup (99524, 0) failed: Permission denied

2018-10-20 11: 25: 00.760 27453-27453 /? E / asset: setgid: Operation not permitted

2018-10-20 11: 25: 01.228 1510-1630 /? E / ANDR-PERF-JNI: com_qualcomm_qtiperformance_native_perf_io_prefetch_start

2018-10-20 11: 25: 23.934 1510-1600 /? E / BatteryExternalStatsWorker: no controller energy info supplied for bluetooth

    
asked by Ruben 19.10.2018 в 11:43
source

1 answer

0

You mention that your ad was showing, so I guess your ad unit ID is valid, but I do not see the need for a " request Agent " to identify the origin of your request, remove the definition from this "request Agent ":

// Load an ad into the AdMob banner view.
 AdView adView= (AdView) getView().findViewById(R.id.adView);
 //AdRequest adRequest = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
 AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                   .addTestDevice("<id desplegado en LogCat>")
                   .build();
 adView.loadAd(adRequest);

I am currently changing the AdMob ad initialization . therefore you must use MobileAds.initialize( ) and review the LogCat to obtain the id of the test device that is required to define in the .addTestDevice() method, this id would display a message in LogCat , example:

  

I / Ads: Use   AdRequest.Builder.addTestDevice ("xxxxxxxxxxxxxxx") to   get test ads on this device.

this would be an example of the complete code:

import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Inicializacion
        MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxx");


       // Load an ad into the AdMob banner view.
       AdView adView= (AdView) getView().findViewById(R.id.adView);
       AdRequest adRequest = new AdRequest.Builder()
                 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                 .addTestDevice("<id desplegado en LogCat>")
                 .build();
      adView.loadAd(adRequest);

    }
    ...
}

You can preferably define the value of the id of your Ad unit within the file strings.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    ...
    ...
    ...
   <string name="banner_ad_unit_id">ca-app-pub-xxxxxxxx/xxxxxxxx</string>
</resources>

obviously you can call your ADMOB_APP_ID (ad unit id) from strings.xml in this way:

MobileAds.initialize(this, getResources().getString(R.string.banner_ad_unit_id));

It is also important to update our file AndroidManifest.xml by defining for the configuration of the application the following label <meta-data>

   <application ....>

        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>

   </application>

I add an example with this update:

link

    
answered by 19.10.2018 в 18:32