显示插页式广告崩溃应用

时间:2015-01-24 17:07:47

标签: java android admob

如果我的应用已完成加载广告并开始显示,则应用会崩溃。

我的GameActivity中的

onCreate方法:

    // Create an ad.
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_UNIT_ID);

    // Set the AdListener.
    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.d(LOG_TAG, "onAdLoaded");
            Toast.makeText(GameActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();

            // Show the ad.
            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
            } else {
                Log.d(LOG_TAG, "Interstitial ad was not ready to be shown.");
            }
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            String message = String.format("Ad failed to load (%s)", getErrorReason(errorCode));
            Log.d(LOG_TAG, message);
            Toast.makeText(GameActivity.this, message, Toast.LENGTH_SHORT).show();
        }
    });

    // Check the logcat output for your hashed device ID to get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();

    // Load the interstitial ad.
    interstitialAd.loadAd(adRequest);    

LogCat说:

01-24 11:33:52.776: E/AndroidRuntime(3072): FATAL EXCEPTION: Thread-147
01-24 11:33:52.776: E/AndroidRuntime(3072): Process: net.obviam.droidz, PID:     3072
01-24 11:33:52.776: E/AndroidRuntime(3072): java.lang.NullPointerException
01-24 11:33:52.776: E/AndroidRuntime(3072):     at  com.android.app.MainGamePanel.render(MainGamePanel.java:276)
01-24 11:33:52.776: E/AndroidRuntime(3072):     at com.android.app.MainThread.run(MainThread.java:44)

MainGamePanel

public void render(Canvas canvas){
    canvas.drawColor(Color.rgb(213, 201, 187)); //(LogCat: MainGamePanel.java:276)
}    

调用方法的MainThread"渲染":

public class MainThread extends Thread{
    private static final String TAG = MainThread.class.getSimpleName();
    private SurfaceHolder surfaceHolder;
    private MainGamePanel gamePanel;
    private boolean running;

    public void setRunning(boolean running){
        this.running = running;
    }

    public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel){
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }

    @Override
    public void run(){
        Canvas canvas;
        Log.d(TAG, "Starting game loop");
        while (running) {
            canvas = null;
            // try locking the canvas
            try{
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder){
                    // update game state 
                    this.gamePanel.update();
                    // render state to the screen
                    // draws the canvas on the panel
                    this.gamePanel.render(canvas);  //(LogCat: MainThread.java:44)
                }
            }finally{
                // in case of an exception the surface is not left in 
                // an inconsistent state
                if(canvas != null){
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }
}

我不知道应用在显示广告时崩溃的原因。没有广告或加载它一切正常。请帮帮我。

问候,杰夫

1 个答案:

答案 0 :(得分:0)

文档说当表面尚未准备好时返回null。也许回调就是你需要的。不确定它是否会起作用但它可能会对您有所帮助:

private Canvas mCanvas;

@Override
public void run(){
    Log.d(TAG, "Starting game loop");
    final Runnable useCanvas = new Runnable() {
        @Override
        public void run() {
            synchronized (surfaceHolder){
                // update game state
                gamePanel.update();
                // render state to the screen
                // draws the canvas on the panel
                gamePanel.render(mCanvas);  //(LogCat: MainThread.java:44)
                surfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    };

    while (running) {
        mCanvas = surfaceHolder.lockCanvas();

        if (mCanvas == null) {
            // Wait for surface
            surfaceHolder.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    mCanvas = holder.lockCanvas();
                    useCanvas.run();
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {}
            });
        } else {
            // Surface already ready, use the canvas
            useCanvas.run();
        }
    }
}