2014-10-04 81 views
0

我跟着LIBGDX關於建立AdMob的教程:https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx錯誤設置AdMob後LIBGDX

和我的 「AndroidLauncher.java」 目前看起來是這樣的:

package com.theashcode.hfcytGame.android; 

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

import com.badlogic.gdx.backends.android.AndroidApplication; 
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; 
import com.google.ads.AdRequest; 
import com.google.ads.AdSize; 
import com.google.android.gms.ads.*; 
import com.theashcode.hfcytGame.Game; 


public class AndroidLauncher extends AndroidApplication { 

AdView adView; 
View gameView; 


@Override public void onCreate (Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
    // Create the layout 
    RelativeLayout 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 Game(), config); 

----線造成問題 - ----

// Create and setup the AdMob view 
    adView = new AdView(this, AdSize.BANNER, "ca-app- pub-000000000000");// Put in your secret key here 
    adView.loadAd(new AdRequest()); 

----行的結束導致的問題------

// 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.ALIGN_PARENT_RIGHT); 

    layout.addView(adView, adParams); 

    // Hook it all up 
    setContentView(layout); 
} 
} 

正如我指出的,我在這條線

// Create and setup the AdMob view 
adView = new AdView(this, AdSize.BANNER, "ca-app- pub-000000000000"); 
adView.loadAd(new AdRequest());  

得到一個錯誤就可以了說,第一行的「構造函數的AdView(AndroidLauncher,adSize時,字符串)是未定義

而在第二行它說

- 「在類型的AdView的方法loadAd(com.google.android.gms.ads.AdRequest)不適用於參數(com.google.ads.AdRequest)

- 「構造的AdRequest()是不可見的」

如果有人可以幫助我,我就明白了很多,謝謝。我對libGDX頗爲陌生。

回答

1

的問題是不是與libGDX但與廣告套餐要導入

不要把谷歌播放-services.jar在/庫/文件夾中。相反,在您的項目中引用google play服務庫。

將庫導入工作區。你會發現它在sdk/extras/google/ ..如果你通過SDK管理器下載。然後右鍵單擊引用您的項目=>選擇Properties => Android的=>添加

然後你需要做以下修改:

變化:

adView = new AdView(this, AdSize.BANNER, "ca-app- pub-000000000000"); 
adView.loadAd(new AdRequest()); 

AdView adView = new AdView(this); 
adView.setAdSize(AdSize.BANNER); 
adView.setAdUnitId("ca-app- pub-000000000000"); 
AdRequest adRequest = new AdRequest.Builder() 
    .build(); 
adView.loadAd(adRequest); 

在清單中的應用程序標記中,更改:

<activity android:name="com.google.ads.AdActivity"/> 

要:

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

地址:

<meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version"/> 

不要忘了權限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
+0

謝謝,libs文件夾中有唯一的「谷歌播放服務。 jar「,我只有這些類現在導入:import com.google.android.gms.ads。在AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; 但是我仍然得到錯誤。謝謝您的幫助。 – Ash 2014-10-04 21:37:59