Android 尝试访问活动时出现nullpointerexception

Android 尝试访问活动时出现nullpointerexception,android,variables,android-activity,nullpointerexception,Android,Variables,Android Activity,Nullpointerexception,当我试图访问我的活动并更改一些整数时,我收到nullpointerexception。 这就是它看起来的样子: MainActivity.java 这是我试图从以下方面接触活动的地方: MainGamePanel.java 这是日志: 11-18 10:23:47.336: D/MainThread(1190): Starting game loop 11-18 10:23:47.336: D/MainThread.initTimingElements()(1190): Timing eleme

当我试图访问我的活动并更改一些整数时,我收到nullpointerexception。 这就是它看起来的样子: MainActivity.java

这是我试图从以下方面接触活动的地方: MainGamePanel.java

这是日志:

11-18 10:23:47.336: D/MainThread(1190): Starting game loop
11-18 10:23:47.336: D/MainThread.initTimingElements()(1190): Timing elements for stats initialised
11-18 10:23:47.386: D/gralloc_goldfish(1190): Emulator without GPU emulation detected.
11-18 10:24:19.925: D/dalvikvm(1190): GC_CONCURRENT freed 356K, 2% free 28540K/28999K, paused 132ms+30ms, total 441ms
11-18 10:24:20.965: W/dalvikvm(1190): threadid=13: thread exiting with uncaught exception (group=0x40a13300)
11-18 10:24:20.965: E/AndroidRuntime(1190): FATAL EXCEPTION: Thread-105
11-18 10:24:20.965: E/AndroidRuntime(1190): java.lang.NullPointerException
11-18 10:24:20.965: E/AndroidRuntime(1190):     at com.nti.hanga.gubbe.MainGamePanel.update(MainGamePanel.java:591)
11-18 10:24:20.965: E/AndroidRuntime(1190):     at com.nti.hanga.gubbe.MainThread.run(MainThread.java:92)
11-18 10:24:21.235: I/AndroidRuntime(1190): VM exiting with result code 0, cleanup skipped.
MainGamePanel.java:591
is
activity.win()


谢谢您的帮助。

如果
MainGamePanel.java
文件的
591
行中有
activity.win()
activity.fail()
,则可能意味着
activity==null
。您确定已初始化它吗?

您甚至不应该尝试从SurfaceView引用您的活动

定义一个接口并使用回调让活动知道游戏结束了

public Interface GameOverListener {
    void onGameOver(boolean won);
}
在自定义曲面视图中

ArrayList<GameOverListener > listeners = new ArrayList<GameOverListener >();

...

public void setGameOverListener(GameOverListener listener){
    listeners.add(listener);
}
在您的活动中:

public class MainActivity extends Activity implements GameOverListener {

...

@Override
public void onCreate(Bundle savedInstanceState) 
{
    ... 
    gamePanel.setGameOverListener(this);
    ...
}

public void onGameOver(boolean won){
   if (won){
      // they won!
   } else {
      // they lost!
   }
}

您可以通过添加removeGameOverListener并检查是否在setGameOverListener中两次添加相同的侦听器来改进surface view类。

正是该活动初始化了GamePanel。该活动应已在启动时初始化。还是我遗漏了什么?我将在问题中向您展示我的onCreate。您从来没有一行
activity=this
,因此
activity
将始终为
null
。没有代码将MainActivity分配给您的“activity”引用谢谢。它起作用了!现在我只需要弄清楚为什么我的SharedReferences在重新开始游戏时没有加载。很高兴听到它有帮助。祝您在共享参考资料方面好运。如果你没有弄明白,开始一个新问题…祝你好运。
ArrayList<GameOverListener > listeners = new ArrayList<GameOverListener >();

...

public void setGameOverListener(GameOverListener listener){
    listeners.add(listener);
}
for (GameOverListener listener:listeners){
   listener.onGameOver(gameWon?true:false); // here you pass true if they won, false if they lost
}
public class MainActivity extends Activity implements GameOverListener {

...

@Override
public void onCreate(Bundle savedInstanceState) 
{
    ... 
    gamePanel.setGameOverListener(this);
    ...
}

public void onGameOver(boolean won){
   if (won){
      // they won!
   } else {
      // they lost!
   }
}