2016-08-05 11 views
0

トランジションのGameOver状態と他のアクティビティとの切り替えに問題があります。アクティビティ間の切り替えでエラーが発生しました

エラーは、「アプリケーションは、停止している」と私は私が望むものを実行し、「OK」をクリックするとされます。

私を助けることができますか? "gameView.updateGameOver();"

エラーが

であります

のJava NullPointerExceptionが

package com.example.asus.game2d; 

import android.content.Context; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.media.SoundPool; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class GameOverDialog extends AppCompatActivity implements View.OnClickListener { 

    //image button 

    private Button ButtonOk; 
    private GameView gameView; 
    private ImageView medaille; 
    private ImageButton LogOut; 
    private ImageButton SoundOff; 
    private ImageButton SoundOn; 
    private SoundPool soundPool; 
    private int soundID; 
    public static boolean play = true; 
    public static int Clicked = 0; 

    private Context mContext; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ///*********end 

     setContentView(R.layout.gameover); 

     //setting the orientation to landscape 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 



     ImageView medaille = (ImageView) findViewById(R.id.medaille); 
     //adding a click listener 

     //Define Logout 
     ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8); 


     //Define SoundOn 

    /* ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9); 

     //Define SoundOff 

     ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10); 
     */ 


     //getting the button 
     ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11); 

     LogOut.setOnClickListener(this); 
     /* SoundOff.setOnClickListener(this); 
     SoundOn.setOnClickListener(this);*/ 
     ButtonOk.setOnClickListener(this); 

     // Version 

     //define 
     TextView txtView = (TextView) findViewById(R.id.tv_current_score); 

     TextView txtsView = (TextView) findViewById(R.id.tv_current_score_value); 
     txtsView.setText(String.valueOf(GameView.score)); 

     //Value 
     TextView myText = (TextView) findViewById(R.id.tv_best_score_value); 
     myText.setText(String.valueOf(GameView.Highscorer)); 

     TextView txtViews = (TextView) findViewById(R.id.tv_best_score); 


    } 

    /* 
    private void soundPlay() 
    { 
     //* Sounds effects 

     // This SoundPool is deprecated but don't worry 
     soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 

     try { 
      // Create objects of the 2 required classes 
      AssetManager assetManager = mContext.getAssets(); 
      AssetFileDescriptor descriptor; 

      // Load our fx in memory ready for use 
      descriptor = assetManager.openFd("gameover.wav"); 

       soundID = soundPool.load(descriptor, 0); 

     } catch (IOException e) { 
      // Print an error message to the console 
      Log.e("error", "failed to load sound files"); 
     } 
     if(play) { 
      soundPool.play(soundID, 1, 1, 0, 0, 1); 
     } 
    }*/ 

    @Override 
    /* 
    public void onClick (View v){ 
     gameView.lives = 3; 
     gameView.run(); 
    } 


    /* 
    */ 

     public void onClick (View v){ 



     switch (v.getId()) { 

       case R.id.imageButton11: 
        //starting game activity 
        gameView.updateGameOver(); 





        break; 

       case R.id.imageButton4: 
        //Logout 
        startActivity(new Intent(this, GameLogout.class)); 
        // do your code 
        break; 
       case R.id.imageButton8: 
        startActivity(new Intent(this, GameLogout.class)); 
        break; 
       /*case R.id.imageButton9: 
        play = false; 
        SoundOn.setVisibility(View.GONE);//set visibility to false on create 
        SoundOff.setVisibility(View.VISIBLE); 
        break; 
       case R.id.imageButton10:*/ 
        /*play = true; 
        SoundOn.setVisibility(View.VISIBLE);//set visibility to false on create 
        SoundOff.setVisibility(View.GONE); 
        break; 
        */ 



      } 

     } 

} 

ジャワ。

コード:

public void nullify() { 

     // Set all variables to null. You will be recreating them in the 
     // constructor. 
     paint = null; 
     lives = 0; 
     enemies = null; 
     boom = null; 
     bosses = null; 
     canvas = null; 
     player = null; 

     envi = null; 
     spikes= null; 
     paint = null; 
     mContext = null; 
     level = 0; 
     score = 0; 

     // Call garbage collector to clean up memory. 
     System.gc(); 

    } 


    public void updateGameOver() { 

        nullify(); 
        Intent intent = new Intent(mContext, MainActivity.class); 
        mContext.startActivity(intent); 
        return; 
       } 

アイデアは、ゲームを再起動するボタンをクリックして行くことです。あなたは「無効()」メソッドでは、それがnullに設定した後

+0

あなたが 'System.gc()'を自分で呼び出す必要はないと言った人はいませんか? – EpicPandaForce

+0

Offtopic。 'void 'メソッドの最後のステートメントであるため、' returnGameOver() 'メソッドの最後に' return; 'は必要ありません。 –

+0

Okしかしこれは答えではありません –

答えて

1

mContextはnullです。 So:

Intent intent = new Intent(mContext, MainActivity.class); 
mContext.startActivity(intent); 

が失敗します。

+0

ゲームを再開します。 –

+0

@PauloRodriguesは 'updateGameOver()'で使用するため、mContextをnullにしません。それで全部です。 –

+0

作業は行っていません。トピックの更新を参照してください。 –

0

として、簡単なあなたのgameViewオブジェクトがnullです。あなたはそれを初期化していません。

また、単にあなたがのonClickメソッドでクリーンアップタスクを行うべきではありません

Intent intent = new Intent(YourActivityName.this, MainActivity.class); 
    startActivity(intent); 
+0

あなたは私を助けることができますか? ? –

+0

ゲームビュークラス – Piyush

+0

あなたは何をしたいですか? –

0

として呼び出します。アクティビティには、これらの種類のタスクに対してonStopメソッドとonDestroyメソッドがあります。

updateGameOver法からNULLIFY()メソッドを削除します。新しいアクティビティが開始されると、次の活動が現在の活動を隠さ場合

は、現在の活動はonStopから行くでしょう。

@Override 
    protected void onStop() { 
      super.onStop(); 
      nullify(); 
    } 
    public void updateGameOver() { 

    // nullify(); 
    Intent intent = new Intent(mContext, MainActivity.class); 
    mContext.startActivity(intent); 
    return; 
    } 
関連する問題