我刚刚完成了Super Jumper教程(来自Beginning Android Games Mario Zechner)几天前,我试图将Admob添加到游戏中。
我按照google提供的指南(使用google-play-services_lib),在点击其中一个开发广告之前,一切似乎都运行良好。
广告按预期打开,但当我关闭它以返回游戏时,我的游戏是黑色的。如果我触摸屏幕就会出现ANR,如果我强行关闭应用程序,游戏就会重新启动...
我发现当我打开广告时会调用onDestroy,并且创建了一个新的游戏实例(onCreate被调用),我想这就是为什么在我强行关闭应用程序后游戏重启的原因。
我不知道为什么会这样,但我尝试在不同的智能手机上运行游戏,我得到了相同的结果。
这是我使用的代码:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.example.jumper.framework.Audio;
import com.example.jumper.framework.FileIO;
import com.example.jumper.framework.Game;
import com.example.jumper.framework.Input;
import com.example.jumper.framework.Screen;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public abstract class GLGame extends Activity implements Game, Renderer {
enum GLGameState {
Initialized,
Running,
Paused,
Finished,
Idle
}
GLSurfaceView glView;
GLGraphics glGraphics;
Audio audio;
Input input;
FileIO fileIO;
Screen screen;
AdView adView;
AdRequest adRequest;
private static String AD_UNIT_ID = "MY_ID_HERE";
GLGameState state = GLGameState.Initialized;
Object stateChanged = new Object();
long startTime = System.nanoTime();
WakeLock wakeLock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
//adView.setAdListener(new AdListenerPage(this));
adView.setBackgroundColor(Color.TRANSPARENT);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(glView);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(adView, params);
setContentView(layout);
adView.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("MY_DEVICE_HERE").build());
glGraphics = new GLGraphics(glView);
fileIO = new AndroidFileIO(getAssets());
audio = new AndroidAudio(this);
input = new AndroidInput(this, glView, 1, 1);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
}
public void onResume() {
super.onResume();
glView.onResume();
wakeLock.acquire();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
glGraphics.setGL(gl);
synchronized(stateChanged) {
if(state == GLGameState.Initialized)
screen = getStartScreen();
state = GLGameState.Running;
screen.resume();
startTime = System.nanoTime();
}
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
GLGameState state = null;
synchronized(stateChanged) {
state = this.state;
}
if(state == GLGameState.Running) {
float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
startTime = System.nanoTime();
screen.update(deltaTime);
screen.present(deltaTime);
}
if(state == GLGameState.Paused) {
screen.pause();
synchronized(stateChanged) {
this.state = GLGameState.Idle;
stateChanged.notifyAll();
}
}
if(state == GLGameState.Finished) {
screen.pause();
screen.dispose();
synchronized(stateChanged) {
this.state = GLGameState.Idle;
stateChanged.notifyAll();
}
}
}
@Override
public void onPause() {
synchronized(stateChanged) {
if(isFinishing())
state = GLGameState.Finished;
else
state = GLGameState.Paused;
while(true) {
try {
stateChanged.wait();
break;
} catch(InterruptedException e) {
}
}
}
wakeLock.release();
glView.onPause();
super.onPause();
}
@Override
public Input getInput() {
return input;
}
@Override
public FileIO getFileIO() {
return fileIO;
}
@Override
public GLGraphics getGraphics() {
return glGraphics;
}
@Override
public Audio getAudio() {
return audio;
}
@Override
public void setScreen(Screen screen) {
if (screen == null)
throw new IllegalArgumentException("Screen must not be null");
this.screen.pause();
this.screen.dispose();
screen.resume();
screen.update(0);
this.screen = screen;
}
@Override
public Screen getCurrentScreen() {
return screen;
}
}
希望你们能帮助我解决这个问题^^
EDIT1:
logcat的:
12-01 17:14:35.329: E/chromium(9767): external/chromium/net/disk_cache/stat_hub.cc:190: [1201/171435:ERROR:stat_hub.cc(190)] StatHub::Init - App com.example.jumper isn't supported.
12-01 17:14:35.329: E/chromium(9767): external/chromium/net/disk_cache/stat_hub.cc:190: [1201/171435:ERROR:stat_hub.cc(190)] StatHub::Init - App com.example.jumper isn't supported.
12-01 17:14:35.990: V/BrowserManagement(9767): PageLoadFinished
12-01 17:14:36.000: V/BrowserManagement(9767): WebviewLoaded
12-01 17:14:36.040: I/Ads(9767): Scheduling ad refresh 60000 milliseconds from now.
12-01 17:14:36.040: I/Ads(9767): Ad finished loading.
12-01 17:14:36.130: D/BrowserManagement(9767): Destroy: unregister BatIntent
12-01 17:14:36.130: D/BrowserManagement(9767): Destroy: unregister BatIntent
12-01 17:14:37.722: D/BrowserManagement(9767): BatIntent Received: 2
12-01 17:14:38.012: D/BrowserManagement(9767): Callstate:0
12-01 17:14:38.012: D/BrowserManagement(9767): ConMgr If:null
12-01 17:14:38.012: E/BrowserManagement(9767): GoDormant p.interface null
12-01 17:14:43.477: V/BrowserManagement(9767): ViewTouched
12-01 17:14:43.487: V/BrowserManagement(9767): ViewTouched
12-01 17:14:43.497: V/BrowserManagement(9767): ViewTouched
12-01 17:14:43.507: V/BrowserManagement(9767): ViewTouched
12-01 17:14:43.527: V/BrowserManagement(9767): ViewTouched
12-01 17:14:43.547: V/BrowserManagement(9767): ViewTouched
12-01 17:14:43.547: V/BrowserManagement(9767): ViewFocusChanged
12-01 17:14:43.918: E/Ads(9767): JS: Uncaught ReferenceError: attributionClick is not defined (http://googleads.g.doubleclick.net/mads/gma:1)
12-01 17:14:43.918: E/Web Console(9767): Uncaught ReferenceError: attributionClick is not defined at http://googleads.g.doubleclick.net/mads/gma:1
12-01 17:14:44.178: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x4cd46000 size:16580608 offset:15810560
12-01 17:14:44.178: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x4e6a5000 size:14266368 offset:13496320
12-01 17:14:44.178: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x4a5ba000 size:3842048 offset:3072000
12-01 17:14:44.178: D/MediaPlayer(9767): pause() mUri is null
12-01 17:14:44.208: I/Ads(9767): Ad opening.
12-01 17:14:44.268: I/Ads(9767): Ad leaving application.
12-01 17:14:44.628: I/Adreno200-EGLSUB(9767): <ConfigWindowMatch:2078>: Format RGBA_8888.
12-01 17:14:44.638: D/memalloc(9767): /dev/pmem: Mapped buffer base:0x50336000 size:21626880 offset:20090880 fd:105
12-01 17:14:44.699: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x50336000 size:21626880 offset:20090880
12-01 17:14:44.709: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x4bd7a000 size:13496320 offset:11960320
12-01 17:14:44.709: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x49b87000 size:7352320 offset:5816320
12-01 17:14:44.719: D/memalloc(9767): /dev/pmem: Unmapping buffer base:0x4de2b000 size:8888320 offset:7352320
12-01 17:14:45.950: E/ActivityThread(9767): Activity com.example.jumper.MainActivity has leaked ServiceConnection com.google.android.gms.common.b@414f9420 that was originally bound here
12-01 17:14:45.950: E/ActivityThread(9767): android.app.ServiceConnectionLeaked: Activity com.example.jumper.MainActivity has leaked ServiceConnection com.google.android.gms.common.b@414f9420 that was originally bound here
12-01 17:14:45.950: E/ActivityThread(9767): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:936)
12-01 17:14:45.950: E/ActivityThread(9767): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:830)
12-01 17:14:45.950: E/ActivityThread(9767): at android.app.ContextImpl.bindService(ContextImpl.java:1129)
12-01 17:14:45.950: E/ActivityThread(9767): at android.content.ContextWrapper.bindService(ContextWrapper.java:370)
12-01 17:14:45.950: E/ActivityThread(9767): at com.google.android.gms.ads.identifier.a.b(SourceFile:330)
12-01 17:14:45.950: E/ActivityThread(9767): at com.google.android.gms.ads.identifier.a.a(SourceFile:187)
12-01 17:14:45.950: E/ActivityThread(9767): at com.google.android.gms.ads.identifier.a.a(SourceFile:173)
12-01 17:14:45.950: E/ActivityThread(9767): at com.google.android.a.u.a(SourceFile:79)
12-01 17:14:45.950: E/ActivityThread(9767): at com.google.android.a.u.doInBackground(SourceFile:75)
12-01 17:14:45.950: E/ActivityThread(9767): at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-01 17:14:45.950: E/ActivityThread(9767): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-01 17:14:45.950: E/ActivityThread(9767): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-01 17:14:45.950: E/ActivityThread(9767): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-01 17:14:45.950: E/ActivityThread(9767): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-01 17:14:45.950: E/ActivityThread(9767): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-01 17:14:45.950: E/ActivityThread(9767): at java.lang.Thread.run(Thread.java:856)
12-01 17:14:46.490: I/Ads(9767): Starting ad request.
12-01 17:14:56.510: A/libc(9767): Fatal signal 6 (SIGABRT) at 0x0000010d (code=0)
12-01 17:15:16.640: I/AdvertisingIdClient(9767): AdvertisingIdClient unbindService failed.
12-01 17:15:16.640: I/AdvertisingIdClient(9767): java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.common.b@414f9420
12-01 17:15:16.640: I/AdvertisingIdClient(9767): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:888)
12-01 17:15:16.640: I/AdvertisingIdClient(9767): at android.app.ContextImpl.unbindService(ContextImpl.java:1159)
12-01 17:15:16.640: I/AdvertisingIdClient(9767): at android.content.ContextWrapper.unbindService(ContextWrapper.java:375)
12-01 17:15:16.640: I/AdvertisingIdClient(9767): at com.google.android.gms.ads.identifier.a.c(SourceFile:275)
12-01 17:15:16.640: I/AdvertisingIdClient(9767): at com.google.android.gms.ads.identifier.b.c(SourceFile:100)
12-01 17:15:16.640: I/AdvertisingIdClient(9767): at com.google.android.gms.ads.identifier.b.run(SourceFile:110)
12-01 17:15:46.719: W/Ads(9767): Timed out waiting for ad response.
12-01 17:15:56.989: W/dalvikvm(9892): VFY: unable to resolve virtual method 180: Landroid/app/Notification$Builder;.setPriority (I)Landroid/app/Notification$Builder;
12-01 17:15:57.189: W/dalvikvm(9892): VFY: unable to resolve static method 3091: Landroid/webkit/WebSettings;.getDefaultUserAgent (Landroid/content/Context;)Ljava/lang/String;
12-01 17:15:57.239: W/dalvikvm(9892): VFY: unable to resolve virtual method 156: Landroid/app/Notification$Builder;.setPriority (I)Landroid/app/Notification$Builder;
12-01 17:15:57.269: D/WebSettings(9892): mDataPath: /data/data/com.example.jumper
12-01 17:15:57.329: I/Ads(9892): Starting ad request.
12-01 17:15:57.429: D/BrowserManagement(9892): BatIntent Received: 2
12-01 17:15:57.480: I/Adreno200-EGLSUB(9892): <ConfigWindowMatch:2078>: Format RGBA_8888.
12-01 17:15:57.480: D/memalloc(9892): /dev/pmem: Mapped buffer base:0x4be94000 size:7352320 offset:5816320 fd:83
12-01 17:15:57.550: I/Adreno200-EGLSUB(9892): <ConfigWindowMatch:2101>: Format RGB_565.
12-01 17:15:57.560: D/memalloc(9892): /dev/pmem: Mapped buffer base:0x4c784000 size:16580608 offset:15810560 fd:89
12-01 17:15:57.630: D/memalloc(9892): /dev/pmem: Mapped buffer base:0x4d950000 size:8888320 offset:7352320 fd:93
12-01 17:15:57.680: D/memalloc(9892): /dev/pmem: Mapped buffer base:0x4e2ca000 size:10424320 offset:8888320 fd:96
12-01 17:15:57.780: D/BrowserManagement(9892): Destroy: unregister BatIntent
12-01 17:15:57.780: D/BrowserManagement(9892): Destroy: unregister BatIntent
12-01 17:15:58.020: V/android_drm_DrmManagerClient(9892): initialize - Enter
12-01 17:15:58.090: V/android_drm_DrmManagerClient(9892): initialize - Exit
12-01 17:15:58.090: V/android_drm_DrmManagerClient(9892): canHandleFd - Enter fd=87
12-01 17:15:58.090: V/android_drm_DrmManagerClient(9892): canHandle - Exit [fd=87] [result=0]
12-01 17:15:58.150: D/MediaPlayer(9892): processDrmFile
12-01 17:15:58.150: V/android_drm_DrmManagerClient(9892): canHandleFd - Enter fd=87
12-01 17:15:58.150: V/android_drm_DrmManagerClient(9892): canHandle - Exit [fd=87] [result=0]
12-01 17:15:58.150: D/MediaPlayer(9892): start() mUri is null
12-01 17:15:58.571: D/memalloc(9892): /dev/pmem: Mapped buffer base:0x4a0b7000 size:3842048 offset:3072000 fd:109
12-01 17:15:58.581: D/memalloc(9892): /dev/pmem: Mapped buffer base:0x4f232000 size:11194368 offset:10424320 fd:112