1

我正在使用 andengine 开发游戏,因此我正在从 BaseGameActivity 扩展我的主要活动。我还实现了 SceneManager。现在从场景管理器中,我想调用一个扩展“活动”的类(不是 BaseGameActivity) 我正在使用以下代码从我的场景管理器中调用该类

class MainActivity extends BaseGameActivity
{
 // code
 // implemented SceneManager
}

class SceneManager
{
  BaseGameActivity activity;

  public void showFbActivity()
  {
    activity.startActivity(new Intent(activity, FbConnect.class)); // Error line
  }
}

class MainMenuScene
{
    public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
        float pMenuItemLocalX, float pMenuItemLocalY) {
    // TODO Auto-generated method stub
//      return false;
      switch(pMenuItem.getID())
      {
      case MENU_PLAY:
        SceneManager.getInstance().loadGameScene(engine);
        return true;
      case MENU_OPTIONS:
        SceneManager.getInstance().showFbActivity();
        return true;
      default:
        return false;
      }
  }
}

class FbConnect extends Activity
{
    protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

现在我的问题是我在上面提到的行上得到了一个空指针异常(参见注释代码)。

以下是上面代码中我的 logcat 输出注释行是 SceneManager 中的第 118 行。

09-14 22:52:32.035: E/AndroidRuntime(22385): FATAL EXCEPTION: UpdateThread
09-14 22:52:32.035: E/AndroidRuntime(22385): java.lang.NullPointerException
09-14 22:52:32.035: E/AndroidRuntime(22385):    at android.content.ComponentName.<init>(ComponentName.java:75)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at android.content.Intent.<init>(Intent.java:2857)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at com.sohi.tech.rollball.SceneManager.showFbActivity(SceneManager.java:118)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at com.sohi.tech.rollball.MainMenuScene.onMenuItemClicked(MainMenuScene.java:93)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.entity.scene.menu.MenuScene.onAreaTouched(MenuScene.java:139)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.entity.scene.Scene.onAreaTouchEvent(Scene.java:413)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:357)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.entity.scene.CameraScene.onSceneTouchEvent(CameraScene.java:64)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.entity.scene.Scene.onChildSceneTouchEvent(Scene.java:420)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:338)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.engine.Engine.onTouchScene(Engine.java:452)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.engine.Engine.onTouchEvent(Engine.java:438)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.input.touch.controller.BaseTouchController$TouchEventRunnablePoolItem.run(BaseTouchController.java:102)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:54)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:1)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.util.adt.pool.PoolUpdateHandler.onUpdate(PoolUpdateHandler.java:88)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.input.touch.controller.BaseTouchController.onUpdate(BaseTouchController.java:62)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.engine.Engine.onUpdate(Engine.java:584)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:56)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
09-14 22:52:32.035: E/AndroidRuntime(22385):    at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)
4

2 回答 2

0
Intent it = new Intent(BaseGameActivity.this, FbConnect.class);
activity.startActivity(it);
于 2013-09-15T07:41:19.930 回答
0

将行更改为

activity.startActivity(new Intent(BaseGameActivity.this, FbConnect.class));
于 2013-09-15T06:50:35.027 回答