2017-04-22 57 views
-1

我一直在嘗試將AdMob橫幅廣告代碼添加到我的應用中。當我嘗試這樣做時,adView.loadAd(adRequest)上出現空指針異常錯誤。Admob代碼在Android Studio中創建空指針異常

這是我的佈局xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

tools:context=".GoldMinerActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/hello_world" /> 

<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    ads:adSize="BANNER" 
    ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView> 

這是我的主要activity.java

public CCGLSurfaceView mGLSurfaceView; 

@Override 

protected void onCreate(Bundle savedInstanceState) { 



    /*super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gold_miner);*/ 

    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    mGLSurfaceView = new CCGLSurfaceView(this); 
    setContentView(mGLSurfaceView); 

    MobileAds.initialize(getApplicationContext(), "ca-app-pub-xxxxxxxxxxxxxx~xxxxxxxxxx"); 
    AdView adView = (AdView) findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    adView.loadAd(adRequest); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_gold_miner, menu); 
    return true; 
} 

@Override 
protected void onStart() { 
    super.onStart(); 

    CCDirector.sharedDirector().attachInView(mGLSurfaceView); 
    getScaledCoordinate(); 

    CCScene scene = CCScene.node(); 
    scene.addChild(new LogoScene(), -1); 
    CCDirector.sharedDirector().runWithScene(scene); 


} 

我想通了這個問題,但不可能解決這個問題。我需要你們大師在這個問題上的專業知識。

由於我使用的

setContentView(mGLSurfaceView); 

我的代碼不能運行我的主XML文件,我得到的錯誤。

但是,如果我用

然後,我在這裏

protected void onStart() { 
    super.onStart(); 

    CCDirector.sharedDirector().attachInView(mGLSurfaceView); 
    getScaledCoordinate(); 

得到一個空指針異常錯誤,因爲我沒有創建mGLSurfaceView中的onCreate。有人可以幫我解決這個問題嗎?

+0

CCGLSurfaceView是一個.java文件。我如何將它添加到佈局?我對此很困惑。 –

回答

0

原因:

/*super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_gold_miner);*/ 

super.onCreate(savedInstanceState); 

requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
mGLSurfaceView = new CCGLSurfaceView(this); 
setContentView(mGLSurfaceView);<---- THIS LINE 

MobileAds.initialize(getApplicationContext(), "ca-app-pub-xxxxxxxxxxxxxx~xxxxxxxxxx"); 
AdView adView = (AdView) findViewById(R.id.adView); 
AdRequest adRequest = new AdRequest.Builder().build(); 
adView.loadAd(adRequest); 

您設置的內容視圖,以不包含AD瀏覽的佈局。如果您想同時顯示廣告並顯示CCGLSurfaceView,請將CCGLSurfaceView作爲自定義視圖添加到activity_gold_miner。基本上是:

  • 你會找到一個看法,是不是在內容視圖
  • 導致它返回null。因爲ID存在,它不會崩潰您的應用程序,但它給你空
  • 這樣就可以進行廣告請求加載到一個空的觀點
  • 這意味着崩潰

爲了解決這個問題,設置contentview轉換爲具有該ID的佈局,這意味着您應該將Surfaceview作爲自定義視圖添加到佈局中。如果你需要的情況下,您只需:

mGLSurfaceView = (CCGLSurfaceView) findViewById(R.id.theIdHere); 

- 編輯 -

當你改變你的佈局添加surfaceview,您的Java代碼應該是這個樣子:

setContentView(R.layout.yourLayout); 
mGLSurfaceView = (CCGLSurfaceView) findViewById(R.id.surfaceView); 
+0

我無法解決的事情是CCGLSurfaceView是一個.java文件,不是一個.xml文件。在這種情況下,我怎樣才能將它添加到佈局中? –

+0

您添加一個XML標籤。像:' Zoe

+0

這兩行我添加到我的主要活動'mGLSurfaceView =(CCGLSurfaceView)findViewById(R.id.MyView); setContentView(R.layout.activity_gold_miner);' 然後我將這些行添加到我的佈局xml文件中。 '' 我沒有得到任何錯誤同時構建apk但當我運行應用程序時,我得到org.cocos2d.opengl.CCGLSurfaceView的膨脹類錯誤。 可能是什麼原因?感謝您的幫助 –