【问题标题】:Java + Android -> Null pointer exception while starting new activityJava + Android -> 开始新活动时出现空指针异常
【发布时间】:2017-07-09 00:33:53
【问题描述】:

当我从游戏活动返回到主要活动时,我得到了 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)

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

@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/tree/master/app/src/main/res/layout

这里有源代码->

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

感谢您的努力:)

【问题讨论】:

  • 我真的不建议使用 lambdas - Java 8 支持仍处于实验阶段。它还大大减少了您可以在这里获得的支持——几乎没有人在 android 标签上使用它。除此之外 - view() 到底是什么?它不是 Android 的一部分,您还没有为我们提供它。但是您的问题肯定会归结为某个未初始化的变量。
  • 方法 view() 可以在这里找到:github.com/konmik/nucleus/blob/…
  • 我要这么说,我的意思是建设性而非批判性 - 您的代码不可读。使代码如此抽象并在这里滥用 RxJava 的效用为零。需要一两天的时间才能充分了解这里发生的事情来调试它。您要做的就是展示一个游戏板。去做。你所拥有的是一场维护噩梦。
  • @GabeSechan 我在生产中使用来自 retrolambda 的 lambda 至少一年,一切都很好(我认识的每个人也在使用它)。我什至无法想象使用没有 lambda 和方法引用的复杂 rxjava 链。
  • @Than 我会避免复杂的 RxJava 链。 RxJava 有一个地方,但它让你的代码更难调试。在这种情况下,我们将讨论几十行易于阅读和理解的代码,而不是多个库和一个错误,没有人可以轻松地告诉他他的实际问题是什么。

标签: java android android-activity nullpointerexception rx-java


【解决方案1】:
com.example.semanticer.unstable.presentation.GameView.showGameBoard(com.example.semanticer.unstable.domain.model.GameBoard)' on a null object reference

表示 showGameBoard 被抛出一个空对象引用,即名为“view”的引用似乎为空。

您可以在此处找到继承的 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 是基类。

【讨论】:

  • 太棒了 :) 如果对您有帮助,请不要忘记接受这个答案。
【解决方案2】:

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

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

game = GameImpl.createNew(6, 4);

需要定义或未正确定义。尝试先看那里然后从那里去。祝你好运! :)

【讨论】:

  • 它在 git 上,在演示文件夹下 :) 无论如何谢谢
  • 错误说在空对象引用上。然而,showGameBoard() 永远不会在名为“game”的引用上调用,因此上述错误不是由该行引起的。
  • 如果 game WAS 为 null,将引发关于 getBoard() 方法(在“游戏”上调用)的错误,而不是关于在视图引用上调用的 showGameBoard() 方法的错误。跨度>
  • 查看参考是有意义的,因为我已经进行了第二次查看。也许等我回到家我会尝试在计算机上找到 git 项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多