トランジションの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に設定した後
あなたが 'System.gc()'を自分で呼び出す必要はないと言った人はいませんか? – EpicPandaForce
Offtopic。 'void 'メソッドの最後のステートメントであるため、' returnGameOver() 'メソッドの最後に' return; 'は必要ありません。 –
Okしかしこれは答えではありません –