我想在我的Android遊戲中實現廣告。我創建了一個名爲Ads的類,我使用下面的方法從另一個類中的另一個類調用它。Android調用廣告類
public class ControlCenter1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control_center1);
public ControlCenter1(Context context)
{
mScene = ParrotsConstent.E_SCENARIO.MENU;
mContext = context;
mScore = new Score();
mSound = new Sound(context);
mTimer = new Timer(ParrotsConstent.MAX_TIME);
mAnimalPic = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mPicBak = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mEffect = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mDisappearToken = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mToken = new ActionTokenPool();
init();
}
public static Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case GAME_OVER_START:
{
CtlTip2 ctl = (CtlTip2) drawTip2.control;
ctl.init(ParrotsConstent.E_TIP.GAMEOVER.ordinal());
mSound.play(ParrotsConstent.E_SOUND.TIMEOVER);
break;
}
case GAME_OVER_END:
{
Ads ads = new Ads();
ads.Adss();
break;
}
}
}
類與谷歌的廣告如下:
public class Ads extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ads);
}
public void Adss()
{
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
onPause();
}
public void onAdClosed() {
onResume();
mScene = ParrotsConstent.E_SCENARIO.RESULT;
}
});
}
public void displayInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
}
沒有廣告遊戲運行流暢。我在運行遊戲之前沒有收到錯誤信息,但是一旦我運行遊戲,並且在遊戲結束時發生以下致命錯誤。
致命異常:主 工藝:PID:2566
java.lang.NullPointerException: Attempt to invoke virtual method android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121) atandroid.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:549)
at android.content.Context.getString(Context.java:476)
at com.gamingtechnology.parrots.Ads.Adss(Ads.java:69)
at com.gamingtechnology.parrots.Core.ControlCenter1$2.handleMessage(ControlCenter1.java:1188)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
你能舉個例子嗎?謝謝 – user3715674
https://developer.android.com/reference/android/app/Activity.html – theblitz
我已按照該鏈接上的說明,因爲它會再次引導您到主頁面,但我無法看到它是如何在我的情況下。你能做到嗎?你需要更多的信息,也許我可以幫你嗎? – user3715674