没有足够的空间在我的游戏中展示广告

时间:2014-02-20 10:59:31

标签: java android admob

我的MainActivity java 是一个简单的游戏,但admod不显示

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);


        // Crear el objeto adView
        adView = new AdView(this, AdSize.BANNER, "a14e2f8fe3af5a6");

        // Buscar LinearLayout suponiendo que se le ha asignado
        // el atributo android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

        // Añadirle adView
        layout.addView(adView);

        // Iniciar una solicitud genérica para cargarlo con un anuncio
        adView.loadAd(new AdRequest());




        // 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
       // mainView = new MainView(this);
        //setContentView(mainView);
       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();

        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;
    }

}

我不知道为什么,但我的activity_main.xml不显示admob .. admob工作和游戏,但toguether不工作(对不起我的英语)

<?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"
              android:id="@+id/mainLayout"
              >

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

</LinearLayout>

谢谢

1 个答案:

答案 0 :(得分:0)

我认为这更容易:

            <com.google.ads.AdView
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                ads:adSize="BANNER"
                ads:adUnitId="a14e2f8fe3af5a6" 
                ads:loadAdOnCreate="true"/>  

在xml中设置它会起作用。

哦,不要忘记清单:

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

我认为这仅适用于插页式广告,但以防万一添加