任何人都可以就此提出一个想法
@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);
}
这是包含一些游戏逻辑的oncreate方法ADN AdMob横幅的代码。 这里出了点问题所以我不知道是什么。 它应该以横向模式显示游戏,底部有横幅。
以下是我遇到的两个错误:
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
所以我的问题是:在游戏屏幕上放置admob的最佳方法是什么?是使用相对布局还是其他?
答案 0 :(得分:1)
忽略它发生在我身上的第一个错误,但播放服务工作正常。对于第二个,您将两个视图一个接一个地进入相同的布局。您可以尝试使用viewGroup或执行类似这样的操作
LinearLayout childLayout =
new LinearLayout(this);
childLayout.setOrientation
(LinearLayout.HORIZONTAL);
childLayout.addView
(secondView);
parentLayout.add(childLayout);
答案 1 :(得分:0)
问题在于:
layout.removeAllViews();
layout.addView(mMainView, adParams);
layout.addView(adView, adParams1);
setContentView(layout);
也许是因为我已经调用了一次这个方法: 的setContentView(R.layout.main);
是否有关于具有FrameLayout
的游戏设置广告视图的示例