Java+;安卓->;启动新活动时出现空指针异常

Java+;安卓->;启动新活动时出现空指针异常,java,android,android-activity,nullpointerexception,rx-java,Java,Android,Android Activity,Nullpointerexception,Rx Java,当我从游戏活动返回到主要活动时,我得到一个NPE。在一个甚至没有被调用的函数中。(当我试图在该行前面的控制台中写入时,它没有任何作用) 这就是我想返回到主要活动的地方-是的,我也尝试了finish() 这是我接到的电话 protected void onCreate(Bundle savedState) { super.onCreate(savedState); game = GameImpl.createNew(6, 4); **view().subscribe(vie

当我从游戏活动返回到主要活动时,我得到一个NPE。在一个甚至没有被调用的函数中。(当我试图在该行前面的控制台中写入时,它没有任何作用)

这就是我想返回到主要活动的地方-是的,我也尝试了finish()

这是我接到的电话

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上->

你可以在这里找到布局-

/zdenduk/androidUnstableAutoms/tree/master/app/src/main/res/layout

和源代码->

/zdenduk/androidunstableautoms/tree/master/app/src/main/java/com/example/semanticer/unstable


感谢您的努力:)

像这样的NullPointerException是未定义变量的结果

如果我不得不从你的代码中猜测(看起来我需要更多的代码才能准确地帮助你),我会说

game = GameImpl.createNew(6, 4);
需要定义或未正确定义。试着先看那里,然后从那里出发。祝你好运!:)

表示showGameBoard在一个空对象引用上抛出,即称为“视图”的引用似乎为空

您可以在此处找到继承的视图变量:

因此,这可能会帮助您:

 /**
     * 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;
}

java是基类。

我真的不建议使用lambdas-java 8支持仍处于实验阶段。它还大大降低了您在这里可以获得的支持——几乎没有安卓标签上的人使用它。除此之外,view()到底是什么?它不是Android的一部分,你也没有提供给我们。但是您的问题肯定会归结为某个未初始化的变量。这里可以找到Method view():我要说的是,我的意思是建设性的,而不是关键性的-您的代码不可读。将代码抽象为抽象代码并在这里滥用RxJava没有任何用处。需要一两天的时间才能充分理解这里发生的事情,从而调试它。你要做的就是展示一个游戏板。那样做。你现在的情况是一场维护噩梦。@Gabeschen我在生产中使用retrolambda的lambdas至少一年了,一切都很好(我认识的每个人都在使用它)。我甚至无法想象在没有lambdas和方法引用的情况下使用复杂的rxjava链。@我会避免使用复杂的rxjava链。RxJava有一席之地,但它使代码的可调试性大大降低。在本例中,我们将讨论几十行易于阅读和理解的代码,而不是多个库,以及一个没有人能轻易告诉他实际问题是什么的bug。它位于git上的presentation文件夹下:)无论如何,谢谢错误显示为空对象引用。但是,showGameBoard()从未在名为“game”的引用上调用过,因此上述错误不能由此行引起。如果game为null,则会抛出一个关于getBoard()方法(在“game”上调用)的错误,而不是关于showGameBoard()方法的错误,这是在视图引用上调用的。视图引用有意义,因为我已经看了第二遍。也许等我回家后,我会在电脑上找到git项目。太好了:)如果这个答案对你有帮助的话,别忘了接受它。
game = GameImpl.createNew(6, 4);
com.example.semanticer.unstable.presentation.GameView.showGameBoard(com.example.semanticer.unstable.domain.model.GameBoard)' on a null object reference
 /**
     * 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;
}