Jalle
Jalle

Reputation: 1624

Need help wiith Activity view and Admob

Can anyone give me an idea on this

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDoSave = true;

// Force landscape and no title for extra room
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);

adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("a15341caf10045c");
AdRequest adRequest = new AdRequest.Builder().build();   
adView.loadAd(adRequest);

// If the user has never accepted the EULA show it again.
mSettings = getSharedPreferences("SolitairePreferences", 0);

//setContentView(R.layout.main);
mMainView = findViewById(R.id.main_view);

mSolitaireView = (SolitaireView) findViewById(R.id.solitaire);
mSolitaireView.SetTextView((TextView) findViewById(R.id.text));

RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

RelativeLayout.LayoutParams adParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams1.addRule(RelativeLayout.CENTER_HORIZONTAL); 

layout.addView(mMainView, adParams);
layout.addView(adView, adParams1);

setContentView(layout);
//StartSolitaire(savedInstanceState);
}

That is oncreate method that contains some game logic ADN the code for AdMob banner. Something is wrong here so I don't know what. It should display game in landscape mode with the banner on the bottom.

Here are two errors I get:

E/GooglePlayServicesUtil(15959): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.



 FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jalle.solitaire/com.jalle.solitaire.Solitaire}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4905)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
at android.view.ViewGroup.addView(ViewGroup.java:3249)
at android.view.ViewGroup.addView(ViewGroup.java:3225)
at com.jalle.solitaire.Solitaire.onCreate(Solitaire.java:93)
at android.app.Activity.performCreate(Activity.java:5244)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
... 11 more

So my question is : What is the best way to place admob on the game screen ? Is it using relative layout or smth else ?

Upvotes: 0

Views: 267

Answers (2)

Maverick
Maverick

Reputation: 971

Ignore the first error it occurs with me too most times but play services works fine. For second one you are two views one after another into the same layout. You can either try using viewGroup or do something like this

LinearLayout childLayout =
new LinearLayout(this);
childLayout.setOrientation
(LinearLayout.HORIZONTAL);
childLayout.addView
(secondView);
parentLayout.add(childLayout);

Upvotes: 1

Jalle
Jalle

Reputation: 1624

problem lies here:

layout.removeAllViews();
layout.addView(mMainView, adParams); 
layout.addView(adView, adParams1);
setContentView(layout); 

maybe its because I already called that method once: setContentView(R.layout.main);

Is there a sample of setting adview on a game that has FrameLayout

Upvotes: 0

Related Questions