我制作了Flappy Bird翻拍,不久前我就完成了它,一切都按预期工作,但当我尝试添加Google的广告时,广告会显示,但游戏无法执行和运行,它是只是一个黑屏。我也在为我的游戏使用LibGDX。这是MainActivity.java代码和Main.xml代码。
MainActivity.java:
package com.kensleeinc.SuperBird;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.kilobolt.ZombieBird.R;
import com.kilobolt.ZombieBird.ZBGame;
public class MainActivity extends AndroidApplication
{
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new ZBGame(), cfg);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_unit_id"/>
</RelativeLayout>
任何人都可以帮我吗?谢谢!
继承我的log.txt -
11-28 19:41:07.521: E/AndroidRuntime(2889): FATAL EXCEPTION: main
11-28 19:41:07.521: E/AndroidRuntime(2889): Process: com.kilobolt.ZombieBird, PID: 2889
11-28 19:41:07.521: E/AndroidRuntime(2889): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kilobolt.ZombieBird/com.kensleeinc.SuperBird.MainActivity}: java.lang.NullPointerException
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.ActivityThread.access$900(ActivityThread.java:173)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.os.Handler.dispatchMessage(Handler.java:102)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.os.Looper.loop(Looper.java:136)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.ActivityThread.main(ActivityThread.java:5579)
11-28 19:41:07.521: E/AndroidRuntime(2889): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 19:41:07.521: E/AndroidRuntime(2889): at java.lang.reflect.Method.invoke(Method.java:515)
11-28 19:41:07.521: E/AndroidRuntime(2889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
11-28 19:41:07.521: E/AndroidRuntime(2889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
11-28 19:41:07.521: E/AndroidRuntime(2889): at dalvik.system.NativeStart.main(Native Method)
11-28 19:41:07.521: E/AndroidRuntime(2889): Caused by: java.lang.NullPointerException
11-28 19:41:07.521: E/AndroidRuntime(2889): at com.kensleeinc.SuperBird.MainActivity.onCreate(MainActivity.java:26)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.Activity.performCreate(Activity.java:5451)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-28 19:41:07.521: E/AndroidRuntime(2889): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
11-28 19:41:07.521: E/AndroidRuntime(2889): ... 11 more
答案 0 :(得分:0)
您的异常是由两次super.onCreate(savedInstanceState);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //Here ***
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new ZBGame(), cfg);
super.onCreate(savedInstanceState); //and Here ***
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
您的onCreate()
方法必须类似于:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new ZBGame(), cfg);
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
答案 1 :(得分:0)
我修好了,就是广告试图覆盖游戏显示,因此一个会显示而另一个则不显示。这是我为LibGDX工作的onCreate方法。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
//setContentView(R.layout.main);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams gameViewParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//gameViewParams.bottomMargin = 150;
requestWindowFeature( Window.FEATURE_NO_TITLE );
View gameView = initializeForView(new ZBGame(), cfg);
layout.addView(gameView, gameViewParams);
AdView adView = new AdView(this);
adView.setAdUnitId("my-ad-unit-here");
adView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(adView, adParams);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
setContentView(layout);
}