2014-02-17 47 views
1

這是我的MainActivity,遊戲 中有mainViewadView是admob(單獨正確工作)。 遊戲和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[] { " [email protected]" }); 
      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。

謝謝。

+0

你的問題是什麼? –

回答

0

用match_parent替換fill_parent。切勿使用fill_parent,因爲它已被棄用。

嘗試將linearLayout和mainView的高度設置爲特定值,如150dip(作爲測試)。他們現在在一起嗎?

你不應該在一般情況下使用System.exit(0),所以想想關閉你的應用程序的另一種方式。通過查看當前的代碼,我不認爲你需要任何類似的東西。

0
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> 

並添加AdmobSDK.jar到您libs文件夾

+0

2014年8月1日,Google Play將停止接受使用舊版獨立Google移動廣告SDK v6.4.1或更低版本的新應用或更新應用。您必須先升級到Google Play版本的Mobile Ads SDK。 –

0

你需要將它添加到佈局 LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);

LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); adView.setLayoutParams之前定義佈局的寬度和高度爲AdMob廣告視圖(鋪設); layout.addView(adView);