我的游戏使用LIBGDX和AdMob显示白色纹理

时间:2014-04-23 00:34:38

标签: android libgdx admob

我已成功添加并欣赏广告到我的游戏中。但是我第一次加载它,并且只是第一次,在我锁定设备或回到家后再次打开游戏时纹理都是白色矩形但游戏仍在运行,我可以听到声音和演员仍然正常工作,但广告显示正常。

PD。我刚刚发现只有在我安装游戏后打开游戏才会发生这种情况。如果我完成安装,关闭安装wizzard(?),然后打开游戏正常,问题不会发生。

我不知道这是什么原因。

我的代码如下:

MainActivity.java

public class MainActivity extends AndroidApplication {
private AdView adView;
RelativeLayout layout;
View gameView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = false;
    cfg.useAccelerometer = false;
    cfg.useCompass = false;

    // Create the layout
    layout = new RelativeLayout(this);

    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    // Create the libgdx View
    gameView = initializeForView(new MyGame(), false);

    // Create and setup the AdMob view
    adView = new AdView(this);
    adView.setAdUnitId("MY ID");
    adView.setAdSize(AdSize.BANNER);
    adView.setBackgroundColor(Color.TRANSPARENT);
    adView.loadAd(new AdRequest.Builder()
    .build());

    // Add the libgdx view
    layout.addView(gameView);

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    adParams.addRule(RelativeLayout.CENTER_IN_PARENT);

    //adView.setVisibility(View.VISIBLE);
    layout.addView(adView, adParams);

    // Hook it all up
    setContentView(layout);

}

的Manifest.xml

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

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" /><application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape"       android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <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|s    mallestScreenSize" />

</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

如果我锁定设备或回家,声音仍然有效。 关闭游戏后,下次我打开它一切都很好。我不知道为什么会发生一次。

我希望有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

LibGdx #initialiseForView设置ContentView。然后,您将在onCreate中再次调用setContentView。 setContentView只应调用一次。那是你的问题。

相关问题