在使用LibGDX创建的游戏中未调用onCreate()

时间:2014-07-29 23:34:48

标签: java android eclipse libgdx

我正在为Android制作一个简单的LibGDX游戏,但是当我在手机上调试时(HTC One m8),只显示黑屏和横幅广告。 LogCat似乎没有在我的代码中显示任何可能影响显示的错误。

我尝试逐步执行代码,看起来我的Game类中的create()在AndroidLauncher.java中初始化游戏时永远不会被调用。

package com.argentum.typinggame.android;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.argentum.typinggame.TypingGame;
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.AdSize;
import com.google.android.gms.ads.AdView;

public class AndroidLauncher extends AndroidApplication {
    private static final String AD_UNIT_ID = //removed;
    private static final String TEST_DEVICE_ID = //removed;
    protected AdView adView;
    protected View gameView;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useCompass = false;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        RelativeLayout layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);

        AdView admobView = createAdView();
        layout.addView(admobView);
        View gameView = createGameView(cfg);
        layout.addView(gameView);

        setContentView(layout);
        startAdvertising(admobView);

    }

    private AdView createAdView() {
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.setId(12345);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        adView.setLayoutParams(params);
        adView.setBackgroundColor(Color.BLACK);
        return adView;
    }

    private View createGameView(AndroidApplicationConfiguration cfg){
        gameView = initializeForView(new TypingGame(), cfg);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.BELOW, adView.getId());
        gameView.setLayoutParams(params);
        return gameView;
    }

    private void startAdvertising(AdView adView){
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(TEST_DEVICE_ID)
        .build();
        adView.loadAd(adRequest);
    }
}

这是我的游戏课程。当踩到这里的任何东西时都会被召唤。

package com.argentum.typinggame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class TypingGame extends Game implements ApplicationListener{

    public SpriteBatch batch;
    public BitmapFont font;
    public MyAdapter input;
    public Preferences scores;
    public Skin skin;


    public float camWidth;
    public float camHeight;     

    @Override
    public void create() {
        skin = new Skin(Gdx.files.internal("uiskin.json"));
        batch = new SpriteBatch();
        font = new BitmapFont();
        scores = Gdx.app.getPreferences("Scores");
        this.setScreen(new MainMenuScreen(this));   
    }

    @Override
    public void render () {
        super.render();
    }

    @Override
    public void dispose() {
        batch.dispose();
        font.dispose();
    }
}

这是我的日志。根据其他几篇文章,可以安全地忽略GooglePlayServicesUtil错误。广告的错误代码:2可能是由于我的手机未连接到网络造成的,因为广告在其他所有测试中都没有显示错误。

I/Adreno-EGL(1511): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066_msm8974_refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066__release_AU ()
I/Adreno-EGL(1511): OpenGL ES Shader Compiler Version: E031.24.00.06
I/Adreno-EGL(1511): Build Date: 02/06/14 Thu
I/Adreno-EGL(1511): Local Branch: 
I/Adreno-EGL(1511): Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066
I/Adreno-EGL(1511): Local Patches: NONE
I/Adreno-EGL(1511): Reconstruct Branch: NOTHING
I/Ads(1511): Starting ad request.
I/Ads(1511): Please set theme of AdActivity to @android:style/Theme.Translucent to enable transparent background interstitial ad.
V/WebViewChromium(1511): Binding Chromium to the background looper Looper (main, tid 1) {42176b30}
I/chromium(1511): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
I/BrowserProcessMain(1511): Initializing chromium process, renderers=0
W/chromium(1511): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
I/Adreno-EGL(1511): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066_msm8974_refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066__release_AU ()
I/Adreno-EGL(1511): OpenGL ES Shader Compiler Version: E031.24.00.06
I/Adreno-EGL(1511): Build Date: 02/06/14 Thu
I/Adreno-EGL(1511): Local Branch: 
I/Adreno-EGL(1511): Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066
I/Adreno-EGL(1511): Local Patches: NONE
I/Adreno-EGL(1511): Reconstruct Branch: NOTHING
I/SensorManager(1511): registerListenerImpl: listener = com.badlogic.gdx.backends.android.AndroidInput$SensorListener@422066e8, sensor = {Sensor name="Accelerometer Sensor", vendor="HTC Group Ltd.", version=1, type=1, maxRange=19.6133, resolution=0.01, power=0.17, minDelay=10000}, delay = 20000, handler = null
I/AndroidInput(1511): sensor listener setup
E/GooglePlayServicesUtil(1511): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
E/GooglePlayServicesUtil(1511): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
E/GooglePlayServicesUtil(1511): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
W/Ads(1511): There was a problem getting an ad response. ErrorCode: 2
I/Ads(1511): Scheduling ad refresh 60000 milliseconds from now.
W/Ads(1511): Failed to load ad: 2

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.argentum.typinggame.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name="com.argentum.typinggame.android.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
       <!-- <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
             />
       -->
    </application>

</manifest>

感谢您的帮助。

更新

通过将我的游戏视图与父级底部和adview下方对齐,我将其高度设置为0.当我将其更改为对齐父级时,它的工作正常。

0 个答案:

没有答案