2016-09-06 9 views
0

私のゲームで請求を修正しようとしています。アイテムを購入すると(広告も削除する必要があります)、画面を読み込んでいるときにゲームがクラッシュしてしまいます。 Main.javaのonResumeアクティビティに何か問題がありますが、何かがAdviewと関係する必要がありますが、私はそれに精通していません。Androidエラー:nullオブジェクト参照でアクティビティAdviewを再開できません

Here is the logcat error: 
    E/AndroidRuntime: FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to resume activity {com.games.finddifference2.hd/com.games.finddifference2.hd.Main}: 
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.a()' on a null object reference 
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3333) 
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3364) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2671) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5769) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.a()' on a null object reference 
    at com.games.finddifference2.hd.Main.onResume(SourceFile:264) 
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) 
    at android.app.Activity.performResume(Activity.java:6691) 
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3318) 

そしてMain.java:

import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.AdView; 
import com.google.android.gms.ads.InterstitialAd; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.DialogInterface.OnClickListener; 
import android.media.AudioManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.AlphaAnimation; 
import android.widget.RelativeLayout; 

import com.tapjoy.TapjoyConnect; 


/** 
* The initial Android Activity, setting and initiating 
*/ 
@SuppressLint("NewApi") 
public class Main extends Activity { 

    /** Our own OpenGL View overridden */ 
    private OpenGLRenderer openGLRenderer; 
    //private final boolean isGoogle = false; 
    public AdView adView; 
    private InterstitialAd interstitial; 

    private RelativeLayout rl; 
    private RelativeLayout.LayoutParams relativeParamsTOP; 
    private RelativeLayout.LayoutParams relativeParamsBOTTOM; 

    private Context mContext; 
    private boolean fullgame = true; 
    private boolean adshown = false; 
    private boolean tapjoy = false; 
    private AdRequest adRequest; 


    public Handler handler = new Handler() { 
     public void handleMessage(Message msg) { 

      Log.i("Handler", "Handler called with code: " + msg); 

      AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f); 
      animation.setDuration(400); 
      animation.setFillAfter(true); 
      animation.setInterpolator(new AccelerateInterpolator()); 
      AlphaAnimation animation2 = new AlphaAnimation(1.0f, 0.0f); 
      animation2.setDuration(400); 
      animation2.setFillAfter(true); 
      animation2.setInterpolator(new AccelerateInterpolator()); 

      switch(msg.what) 
      { 
      case 0: 
       try { 
        adView.startAnimation(animation2); 
        adView.setVisibility(View.GONE); 
        adshown = false; 
        this.postDelayed(new Runnable() { public void run() { 
         try { 
          //adshown = false; 
          rl.removeView(adView); 
         } catch (Exception e) { e.printStackTrace(); } 
        } }, 400); 
       } catch (Exception e) { e.printStackTrace(); } 
       break; 
      case 1: 
       if (!fullgame && !adshown) { 
        try { 
         adshown = true; 
         rl.addView(adView, relativeParamsBOTTOM); 
         adView.startAnimation(animation); 
         adView.setVisibility(View.VISIBLE); 
        } catch (Exception e) { e.printStackTrace(); } 
       } 
       break;   
      case 2: 
       if (!fullgame && !adshown) { 
        try { 
         adshown = true; 
         rl.addView(adView, relativeParamsTOP); 
         adView.startAnimation(animation); 
         adView.setVisibility(View.VISIBLE); 
        } catch (Exception e) { e.printStackTrace(); } 
       } 
       break; 
      case 3: 
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
       break; 
      case 4: 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
       break; 

      case 5: 
       if (interstitial.isLoaded()) { 
         interstitial.show(); 
       } 

        break; 

       default: 
        break; 
       } 
     } 
    }; 

    /** 
    * Initiate the OpenGL View and set our own 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mContext = this; 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     rl = new RelativeLayout(this); 
     relativeParamsTOP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     relativeParamsBOTTOM = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

     relativeParamsTOP.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     relativeParamsBOTTOM.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 

     this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
     SoundManager.getInstance(); 
     SoundManager.initSounds(this); 
     SoundManager.loadSounds(); 

     SharedPreferences pref = mContext.getSharedPreferences("Settings", Context.MODE_PRIVATE); 
     fullgame = pref.getBoolean("Ads", false); 

     // Create the adView 
     if (!fullgame) { 
      adView = new AdView(this); 
      adView.setAdUnitId(getString(R.string.admob_banner_id)); 
       adView.setAdSize(AdSize.SMART_BANNER); 

       // Create the interstitial. 
       interstitial = new InterstitialAd(this); 
       interstitial.setAdUnitId(getString(R.string.admob_interstitial_id)); 
     } 
     // TAPJOY 
     try { 
      TapjoyConnect.requestTapjoyConnect(this, "3a62180b-8dd2-487c-9af3-30e6455f562b", "X0J1DTeFNZ95yMAdcx8w"); 
      Log.i("Tapjoy", "Tapjoy connected"); 
      tapjoy = true; 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     try { 
      openGLRenderer = new OpenGLRenderer(this, handler); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     // Add the adView to it 
     rl.addView(openGLRenderer); 

     if (!fullgame) { 
      try { 
      adView.setVisibility(View.GONE); 
      adView.loadAd(new AdRequest.Builder().build()); 
      adRequest = new AdRequest.Builder().build(); 
      interstitial.loadAd(adRequest); 
      Log.i("Ads", "----- request loaded"); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     setContentView(rl); 
    } 

    /** 
    * Remember to resume the glSurface 
    */ 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     openGLRenderer.onResume(); 
     adView.resume(); 

     Log.i("OpenGL", "onResume called"); 

      if (interstitial != null && adRequest != null) { 
      interstitial.loadAd(adRequest); 
      } else { 
      interstitial = new InterstitialAd(this); 
      interstitial.setAdUnitId(getString(R.string.admob_interstitial_id)); 
      adRequest = new AdRequest.Builder().build(); 
      interstitial.loadAd(adRequest); 
      } 

    } 

    /** 
    * Also pause the glSurface 
    */ 
    @Override 
     public void onStart() { 
     super.onStart(); 
     final SharedPreferences settings = 
      getSharedPreferences("localPreferences", MODE_PRIVATE); 
     if (settings.getBoolean("isFirstRun", true)) { 

      new AlertDialog.Builder(this) 
       .setMessage("We use device identifiers and share it with our partners to improve your user experience. See details in Privacy Policy.") 
       .setNeutralButton ("Privacy Policy", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick (DialogInterface dialog, int which) { 
         openBrowserLink (Main.this, "http://test.com"); 
        } 
       }) 
       .setPositiveButton ("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         settings.edit().putBoolean("isFirstRun", false).commit(); 
        } 
       }).show(); 
     } 
     } 

     public static void openBrowserLink (Context c, String str) { 
     Intent i = new Intent (Intent.ACTION_VIEW); 
     i.setData (Uri.parse (str)); 
     c.startActivity (i); 
    } 

    @Override 
    protected void onPause() { 
     adView.pause(); 

     openGLRenderer.onPause(); 
     Log.i("OpenGL", "onPause called"); 
     super.onPause(); 
    } 

    @Override 
    protected void onDestroy() { 
     try { adView.destroy(); } catch (Exception e) { e.printStackTrace(); } 
     try { openGLRenderer.onDestroy(); } catch (Exception e) { e.printStackTrace(); } 
     try { SoundManager.cleanup(); } catch (Exception e) { e.printStackTrace(); } 
     if (tapjoy) try { TapjoyConnect.getTapjoyConnectInstance().sendShutDownEvent(); } catch (Exception e) { e.printStackTrace(); } 
     super.onDestroy(); 
    } 


@Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 

      if (!openGLRenderer.mHelper.handleActivityResult(requestCode, resultCode, data)) { 

        super.onActivityResult(requestCode, resultCode, data); 
      } 

     } 

} 

答えて

1

私はわからないが、私はadViewが定義されていないとき、それはだと思います。 onCreateで、fullgame = true - > adViewが定義されていない場合、onResume内のadView.resume()とadViewがnullです。

+0

Defintely。 adViewを使用するたびにフルゲームをリクエストする必要があります。 – Squirrelkiller

関連する問題