当我从游戏活动回到主要活动时,我得到一个NPE。在一个甚至没有调用的函数中。 (当我尝试在该行之前在控制台中写入时,它什么都不做)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.semanticer.unstable.presentation.GameView.showGameBoard(com.example.semanticer.unstable.domain.model.GameBoard)' on a null object reference
at com.example.semanticer.unstable.presentation.GamePresenter.lambda$onCreate$0(GamePresenter.java:23)
这是我想要返回主要活动的地方 - 是的,我也尝试了完成()。
@Override
public void playAgain(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
这是我在
上得到错误的一行protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
game = GameImpl.createNew(6, 4);
**view().subscribe(view -> view.showGameBoard(game.getBoard()));**
view().subscribe(view -> view.showCurrentPlayer(Player.FIRST_PLAYER));
view().subscribe(view -> view.showScore(Player.FIRST_PLAYER, Player.SECOND_PLAYER, game));
view().subscribe(view -> view.hideWinnerText());
}
整个项目在github上 - > https://github.com/zdenduk/AndroidUnstableAtoms
你可以在这里找到布局 -
/ zdenduk / AndroidUnstableAtoms /树/主/应用程序/ SRC /主/ RES /布局
和此处的源代码 - >
/ zdenduk / AndroidUnstableAtoms /树/主/应用程序/ SRC /主/ JAVA / COM /示例/ semanticer /不稳定
感谢您的努力:)
答案 0 :(得分:1)
com.example.semanticer.unstable.presentation.GameView.showGameBoard(com.example.semanticer.unstable.domain.model.GameBoard)' on a null object reference
表示showGameBoard被抛出一个空对象引用,即名为“view”的引用似乎是null。
您可以在此处找到继承的 view 变量: https://github.com/konmik/nucleus/blob/249ab08547cb2b10ddce268dfb0b27b5013623c2/nucleus/src/main/java/nucleus/presenter/Presenter.java
所以这可能对你有帮助:
/**
* Returns a current view attached to the presenter or null.
*
* View is normally available between
* {@link Activity#onResume()} and {@link Activity#onPause()},
* {@link Fragment#onResume()} and {@link Fragment#onPause()},
* {@link android.view.View#onAttachedToWindow()} and {@link android.view.View#onDetachedFromWindow()}.
*
* Calls outside of these ranges will return null.
* Notice here that {@link Activity#onActivityResult(int, int, Intent)} is called *before* {@link Activity#onResume()}
* so you can't use this method as a callback.
*
* @return a current attached view.
*/
@Nullable
public View getView() {
return view;
}
Presenter.java是基类。
答案 1 :(得分:0)
像这样的NullPointerException是未定义变量的结果。
如果我不得不从你的代码猜测(看起来我需要更多它来准确帮助)我会说
game = GameImpl.createNew(6, 4);
需要定义或未正确定义。先尝试一下然后去那里。祝好运! :)