2017-04-22 62 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的膨胀类错误。 可能是什么原因?感谢您的帮助 –