我正在使用andengine开发游戏,所以我从BaseGameActivity扩展了我的主要活动。我还实现了SceneManager。现在从scenemanager内,我想调用扩展“ Activity”的类(不是BaseGameActivity)
我正在使用以下代码从我的SceneManager调用类

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);
}
}


现在我的问题是我在上述行上得到了一个nullpointerexception(请参阅注释的代码)。

以下是我的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)

最佳答案

更改为

activity.startActivity(new Intent(BaseGameActivity.this, FbConnect.class));

07-27 13:50