i can´t see my game.. i can´t display my game and admob in the same layout

45 views
Skip to first unread message

ignacio rodriguez

unread,
Feb 11, 2014, 1:08:10 PM2/11/14
to google-adm...@googlegroups.com
my MainActivity.java



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

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

// Apago la barra de título
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);

setContentView(R.layout.activity_main);
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);

}

@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[] { " trabajo...@gmail.com" });
startActivity(intent);
}
return false;
}

}


my activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:launchMode="singleInstance" 
    tools:context=".MainActivity" >

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

</LinearLayout>

</RelativeLayout>


i need to mix in the same screen but i don´t know how.
I need to change setContentView for see in xml

thank you

Eric Leichtenschlag

unread,
Feb 13, 2014, 11:21:09 AM2/13/14
to google-adm...@googlegroups.com
Hi Ignacio,

You're calling setContentView twice:

setContentView(mainView);
.....
setContentView(R.layout.activity_main);

When this happens, your second setContentView is overriding your first one, so MainView is no longer in the view hierarchy. I'd recommend putting a <MainView> element inside your XML under the RelativeLayout tag.

Thanks,
Eric

ignacio rodriguez

unread,
Feb 17, 2014, 3:49:18 AM2/17/14
to google-adm...@googlegroups.com
but how?, if i send you my proyect you can put admob.. thank you

Eric Leichtenschlag

unread,
Feb 18, 2014, 1:16:38 PM2/18/14
to google-adm...@googlegroups.com
MainView looks like a custom view, so I'm not sure exactly what the XML for that would look like. So you can try this in code. Try adding this to the bottom of your onCreate:

    // Create a top-level layout.
    LinearLayout layout = new LinearLayout(this);
    // Add the AdView to the top of the layout.
    LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.addView(adView, adParams);
    // Next add the MainView.
    LinearLayout.LayoutParams mainViewParams = new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    layout.addView(mainView, mainViewParams);
    // Set the content to be this top-level layout.
    setContentView(layout);

Then you can get rid of your XML file and not add the AdView to your R.id.LinearLayout.

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages