java - Admob 不起作用

标签 java android admob

这是我的MainActivity,游戏中有一个mainView adView 是 admob(单独工作正常)。 游戏和admob分别可以正常工作,但不能一起使用。

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

这是我的 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>

我在同一布局中看不到 mainViewadView 我需要在同一屏幕中进行混合,但我不知道如何进行。 我需要将 setContentView 更改为 xml 格式。

谢谢。

最佳答案

将所有地方的 fill_parent 替换为 match_parent。切勿使用 fill_parent,因为它已被弃用。

尝试将 LinearLayout 和 mainView 的高度设置为特定值,例如 150dip(作为测试)。他们现在一起演出吗?

一般情况下,您不应该使用 System.exit(0),因此请考虑另一种关闭应用程序的方法。通过查看当前的代码,我认为您无论如何都不需要类似的东西。

关于java - Admob 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21827289/

相关文章:

java - Camel 示例找不到 org.apache.camel.impl.DefaultComponent

Android - Sugar ORM 保存崩溃

android - 如何从 Jetpack Compose 测试 @Model 数据类?

android - 无论小部件结构如何,如何确保按钮关闭应用程序?

java - 在 Clojure 中获取日期和时间的最佳方法是什么?

java - 切换 JCheckBox 值

android - AdMob Android 在程序不可见时请求广告

android - 在按下返回按钮后显示插页广告?

android - AdMob - 创建广告时为 "Invalid or non-existent app url"

java - 如何从 Java 应用程序中确定 JVM 最小堆大小?