简体   繁体   中英

Admob doesn't work

This is my MainActivity , there is a mainView in the game and the adView is the admob (work correctly alone). Game and admob works fine respectively, but not together.

package net.canarolab.puzzleroad;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainActivity extends Activity {
    // private AdView adView;
    private MainView mainView;
    private final int MENU_SELECT_RESET = 1, MENU_SELECT_CONTACT = 2;

    // MUSICA
    MediaPlayer mediaPlayer;// para musica de fondo (se declara aqui para que
                            // pueda

    // utilizarla todos nuestros metodos)

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

        //ADMOB
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        // Create the adView
        // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
        AdView adView = new AdView(this, AdSize.BANNER, "a14e2f8fe3af5a6");
        // Add the adView to it
        layout.addView(adView);
        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        request.setTesting(true);
        adView.loadAd(request);


        // MUSIC
        mediaPlayer = MediaPlayer.create(this, R.raw.merry);
        mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(100, 100);
        mediaPlayer.start();


        //MAINVIEW
        // TURN OFF THE TITLE
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Apago la barra de estado
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Intent i = getIntent();
        // Activity Quiero solo tapa
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        // View Establezca el

        RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.mainView);
        mainView = new MainView(this);
        layout1.addView(mainView);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.activity_main, menu);
        menu.add(0, MENU_SELECT_RESET, 0, "Reset");
        menu.add(0, MENU_SELECT_CONTACT, 0, "Contact");
        return true;

    }

    @Override
    protected void onResume() {
        mediaPlayer.start();

        // Leí el recuento juego
        this.mainView.gameCount.read();
        Log.d("", "read");
        super.onResume();

    }

    @Override
    protected void onPause() {
        mediaPlayer.pause();

        // Escribo el recuento de juego.
        this.mainView.gameCount.save();
        Log.d("", "save");

        super.onPause();
        // No voy a desaparecer en esta actitud no hay más.
        // finish();

    }

    @Override
    protected void onDestroy() {
        mediaPlayer.stop();

        super.onDestroy();
        System.exit(0);

    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_SELECT_RESET:
            this.mainView.gameCount.reset();
            Toast.makeText(this, "Has been reset.", Toast.LENGTH_SHORT).show();
            return true;
        case MENU_SELECT_CONTACT:
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, "idea");
            intent.putExtra(Intent.EXTRA_TEXT, "text of email");
            intent.putExtra(Intent.EXTRA_EMAIL,
                    new String[] { " trabajonacho33@gmail.com" });
            startActivity(intent);
        }
        return false;
    }}

and this is my activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/mainView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </RelativeLayout>

</LinearLayout>

I can't see the mainView and adView in the same layout. I need to mix in the same screen but I don't know how. I need to change setContentView to be in xml.

Thank you.

Replace fill_parent with match_parent everywhere. Never use fill_parent, as it is deprecated.

Try to set the height of linearLayout and mainView to a specific value like 150dip (as a test). Do they show together now?

You should not use System.exit(0) in general, so think of another way of closing you app. By looking at the current code, I don't think you need anything like that anyway.

You can use layout to show your add.Just put below code in your layout where ever you want into the screen.
       <RelativeLayout
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_alignParentBottom="true" >

     <com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="Your AddMob ID"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE" />  
    </RelativeLayout>

And add AdmobSDK.jar into you libs folder

You need to define the layout width and height for the admob view before adding it to the layout LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);

LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); adView.setLayoutParams(lay); layout.addView(adView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM