我一直在嘗試將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文件。我如何將它添加到佈局?我對此很困惑。 –