我目前正在为派对游戏“米勒斯谷的狼人”编写游戏应用程序。当游戏中的任何角色都要做出决定时,都会创建一个包含所有必要信息的新片段。这一切正常,直到我第二次开始游戏,然后在第一次提交时抛出IllegalStateException
我已经尝试使用commitAllowingStateLoss()
,但这只会导致不可见的UI。从片段管理器中删除所有片段也无济于事。
“我的应用”以主要活动作为主要“菜单”开始。
public class MainActivity extends AppCompatActivity {
//Some Code
private void startGame(View view) {
Intent intent = new Intent(this, GameActivity.class);
//Some extras
startActivity(intent);
}
}
这将调用以下GameActivity:
public class GameActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.game_layout.xml); //only contains the fragment container
new GameTast().execute();
}
//IOMethod is an interface the game calls when something should be displayed or input is requested
class GameTask extends Asynctask<Void,Void,Void> implements IOMethod {
@Override
protected Void doInBackground(Void... voids){
//Starting my game and waiting for finish
Intent intent = new Intent(GameActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
//Many methods for different views, e.g.
public List<Character> promptForCharacterSelection(/*Backend params*/){
getSupportFragmentManager().beginTransaction().replace(R.id.gameFrame, new IngameCharacterSelectionFragment()).commit();
//wait for user to finish input
}
}
}
游戏第一次执行时运行完美,但是在第二次运行时,我得到以下堆栈跟踪:
2019-07-02 10:17:10.261 4392-4439/com.example.werewolves E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.werewolves, PID: 4392
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2080)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2106)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.example.werewolves.GameActivity$GameTask.promptForPlayerSelection(GameActivity.java:167)
at com.example.werewolvesgame.ClassicMethod.preStart(ClassicMethod.java:47)
at com.example.werewolvesgame.PlayingMethod.start(PlayingMethod.java:20)
at com.example.werewolvesgame.Game.start(Game.java:65)
at com.example.werewolvesgame.Manager.newGame(Manager.java:42)
at com.example.werewolves.GameActivity$GameTask.doInBackground(GameActivity.java:105)
at com.example.werewolves.GameActivity$GameTask.doInBackground(GameActivity.java:97)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)