我一直在尝试将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。有人可以帮我解决这个问题吗?
CCGLSurfaceView是一个.java文件。我如何将它添加到布局?我对此很困惑。 –