W / System.err:java.lang.NullPointerException:尝试调用虚方法' android.app.ActivityThread $ ApplicationThread android.app.ActivityThread.getApplicationThread()'在空对象引用上
W / System.err:在android.app.Activity.startActivityForResult(Activity.java:4226) W / System.err:在android.app.Activity.startActivityForResult(Activity.java:4183) W / System.err:在android.app.Activity.startActivity(Activity.java:4522) W / System.err:在android.app.Activity.startActivity(Activity.java:4490) W / System.err:at com.dividedbyzerostudios.candycrawler.GameplayScene.update(GameplayScene.java:153) W / System.err:at com.dividedbyzerostudios.candycrawler.SceneManager.update(SceneManager.java:29) W / System.err:at com.dividedbyzerostudios.candycrawler.GamePanel.update(GamePanel.java:81) W / System.err:at com.dividedbyzerostudios.candycrawler.MainThread.run(MainThread.java:44)
我在游戏中使用场景管理器,游戏本身就是一个场景,结果是另一个场景。现场经理是这样的:
public class SceneManager {
private ArrayList<Scene> scenes = new ArrayList<>();
public static int ACTIVE_SCENE;
public SceneManager() {
ACTIVE_SCENE = 0;
scenes.add(new GameplayScene());
scenes.add(new result());
}
public void recieveTouch(MotionEvent event) {
scenes.get(ACTIVE_SCENE).recieveTouch(event);
}
public void update() {
scenes.get(ACTIVE_SCENE).update();
}
public void draw(Canvas canvas) {
scenes.get(ACTIVE_SCENE).draw(canvas);
}
}
每个场景实现场景类:
public interface Scene {
public void update();
public void draw(Canvas canvas);
public void terminate();
public void recieveTouch(MotionEvent event);
}
游戏场景工作正常,但是当我将ACTIVE_SCENE切换为1(当游戏结束时)并且意图运行结果活动(下面)时,它会忽略结果活动中的onCreate方法(见后): / p>
public class result extends AppCompatActivity implements Scene {
@Override
protected void onCreate(Bundle savedInstanceState) { /*This is being ignored*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView scoreLabel = (TextView) findViewById(R.id.scoreLabel);
TextView highScoreLabel = (TextView) findViewById(R.id.highScoreLabel);
int score = Constants.SCORE;
scoreLabel.setText(score + "");
SharedPreferences settings = getSharedPreferences("GAME_DATA", Context.MODE_PRIVATE);
int highScore = settings.getInt("HIGH_SCORE", 0);
if(score > highScore) {
highScoreLabel.setText("High Score : " + score);
//save
SharedPreferences.Editor editor = settings.edit();
editor.putInt("HIGH_SCORE", score);
editor.commit();
}
else {
highScoreLabel.setText("High Score : " + highScore);
}
}
public void tryAgain() { /*How I plan to change back to the game*/
SceneManager.ACTIVE_SCENE = 0;
}
public void update() {
}
public void draw(Canvas canvas) {
}
public void terminate() {
}
public void recieveTouch(MotionEvent event) {
}
}
以下是我如何调用活动并更改场景:
public class GameplayScene extends Activity implements Scene {
@Override
public void update() {
if(gameOver) {
//
SceneManager.ACTIVE_SCENE = 1; //changing the scene
Intent intent = new Intent(CURRENT_CONTEXT, result.class);
intent.putExtra("SCORE", Constants.SCORE);
startActivity(intent);
}
我已经尝试不将结果页面设为场景并尝试从意图中运行它,但它只会导致游戏结束冻结。 目前看来它除了onCreate之外还运行结果类中的所有代码。