"Приложение <имя задачи> неожиданно остановлено" в Анденжине

У меня есть 2 файла:

MainActivity.java

public class MainActivity extends BaseGameActivity implements
    IOnMenuItemClickListener {

/*
 * Constants for frame
 */
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

protected static final int MENU_ABOUT = 0;
protected static final int MENU_QUIT = MENU_ABOUT + 1;
protected static final int MENU_PLAY = 100;
protected static final int MENU_SCORES = MENU_PLAY + 1;
protected static final int MENU_OPTIONS = MENU_SCORES + 1;
protected static final int MENU_HELP = MENU_OPTIONS + 1;

/*
 * Fields
 */
private Camera mCamera;
private Scene mMainScene;

// Save Image for Menu
private BitmapTextureAtlas mMenuBackTexture;
private TextureRegion mMenuBackTextureRegion;

private BitmapTextureAtlas mPopUpTexture;
// private BitmapTextureAtlas mFontTexture;

protected Font mFont;

protected TextureRegion mPopUpAboutTextureRegion;
protected TextureRegion mPopUpQuitTextureRegion;
protected TextureRegion mMenuPlayTextureRegion;
protected TextureRegion mMenuScoresTextureRegion;
protected TextureRegion mMenuOptionsTextureRegion;
protected TextureRegion mMenuHelpTextureRegion;

private boolean popupDisplayed;
protected MenuScene mStaticMenuScene, mPopUpMenuScene;

/*
 * Constructors
 */

/*
 * Getter and Setter
 */
public Camera getmCamera() {
    return mCamera;
}

public void setmCamera(Camera mCamera) {
    this.mCamera = mCamera;
}

/*
 * Methods for SuperClass/Interface
 */

@Override
public Engine onLoadEngine() {
    // TODO Auto-generated method stub
    // this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    this.setmCamera(new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT));
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
            this.getmCamera()));
}

@Override
public void onLoadResources() {
    // TODO Auto-generated method stub
    // this.mTexture = new BitmapTextureAtlas(512, 512,
    // TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    // this.mSplashTextureRegion = BitmapTextureAtlasTextureRegionFactory
    // .createFromAsset(this.mTexture, this, "gfx/Splashscreen.png",
    // 0, 0);
    // this.mEngine.getTextureManager().loadTexture(this.mTexture);

    // Process and load Font Menu
    final ITexture mFontTexture = new BitmapTextureAtlas(256, 256,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    FontFactory.setAssetBasePath("font/");

    this.mFont = FontFactory.createFromAsset(mFontTexture, this,
            "Vera.ttf", 32, true, Color.RED);
    this.mFont.reload();
    this.mEngine.getTextureManager().loadTexture(mFontTexture);
    this.mEngine.getFontManager().loadFont(this.mFont);

    // Process image background
    this.mMenuBackTexture = new BitmapTextureAtlas(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    this.mMenuBackTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mMenuBackTexture, this,
                    "gfx/MainMenu/MainMenuBk.png", 0, 0);
    this.mEngine.getTextureManager().loadTexture(this.mMenuBackTexture);

    // Process image PopUp
    this.mPopUpTexture = new BitmapTextureAtlas(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mPopUpAboutTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mPopUpTexture, this,
                    "gfx/MainMenu/About_button.png", 0, 0);
    this.mPopUpAboutTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mPopUpTexture, this,
                    "gfx/MainMenu/Quit_button.png", 0, 50);

    this.mEngine.getTextureManager().loadTexture(this.mPopUpTexture);
    popupDisplayed = false;
}

@Override
public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // final Scene scene = new Scene();

    this.createStaticMenuScene();
    this.createPopUpMenuScene();

    /* Center the splash on the camera. */
    final int centerX = (CAMERA_WIDTH - this.mMenuBackTextureRegion
            .getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mMenuBackTextureRegion
            .getHeight()) / 2;

    this.mMainScene = new Scene();

    /* Create the sprite and add it to the scene. */
    final Sprite menuBack = new Sprite(centerX, centerY,
            this.mMenuBackTextureRegion);
    mMainScene.attachChild(menuBack);
    return mMainScene;
}

@Override
public void onLoadComplete() {
    // TODO Auto-generated method stub

}

@Override
public boolean onKeyDown(int pKeyCode, KeyEvent pEvent) {
    if (pKeyCode == KeyEvent.KEYCODE_MENU
            && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (popupDisplayed) {
            // Remove the menu and reset it.
            this.mPopUpMenuScene.back();
            mMainScene.setChildScene(mStaticMenuScene);
            popupDisplayed = false;
        } else {
            this.mMainScene.setChildScene(mPopUpMenuScene, false, true,
                    true);
            popupDisplayed = true;
        }
        return true;
    } else {
        return super.onKeyDown(pKeyCode, pEvent);
    }
}

@Override
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
        float pMenuItemLocalX, float pMenuItemLocalY) {
    switch (pMenuItem.getID()) {
    case MENU_ABOUT:
        Toast.makeText(MainActivity.this, "About selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_QUIT:
        this.finish();
        return true;
    case MENU_PLAY:
        Toast.makeText(MainActivity.this, "Play selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_SCORES:
        Toast.makeText(MainActivity.this, "Scores selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_OPTIONS:
        Toast.makeText(MainActivity.this, "Options selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_HELP:
        Toast.makeText(MainActivity.this, "Help selected",
                Toast.LENGTH_SHORT).show();
        return true;
    default:
        return false;
    }
}

/*
 * Methods for this class
 */
protected void createStaticMenuScene() {
    // put camera to menu
    this.mStaticMenuScene = new MenuScene(this.mCamera);

    // Add Play game
    final IMenuItem playMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_PLAY, mFont, "Play Game"), 0.5f, 0.5f,
            0.5f, 0.0f, 0.0f, 0.0f);
    playMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(playMenuItem);

    // Add Scores
    final IMenuItem scoresMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_SCORES, mFont, "Scores"), 0.5f, 0.5f,
            0.5f, 0.0f, 0.0f, 0.0f);
    scoresMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(scoresMenuItem);

    // Add Options
    final IMenuItem optionsMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_OPTIONS, mFont, "Options"), 0.5f, 0.5f,
            0.5f, 0.0f, 0.0f, 0.0f);
    optionsMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(optionsMenuItem);

    // Add Help
    final IMenuItem helpMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_HELP, mFont, "Help"), 0.5f, 0.5f, 0.5f,
            0.0f, 0.0f, 0.0f);
    helpMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(helpMenuItem);

    // build camera
    this.mStaticMenuScene.buildAnimations();
    this.mStaticMenuScene.setBackgroundEnabled(false);
    this.mStaticMenuScene.setOnMenuItemClickListener(this);
}

protected void createPopUpMenuScene() {
    this.mPopUpMenuScene = new MenuScene(this.mCamera);

    //add about popup
    final SpriteMenuItem aboutMenuItem = new SpriteMenuItem(MENU_ABOUT,
            this.mPopUpAboutTextureRegion);
    aboutMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mPopUpMenuScene.addMenuItem(aboutMenuItem);

    //add quit popup
    SpriteMenuItem quitMenuItem = new SpriteMenuItem(MENU_QUIT,
            this.mPopUpQuitTextureRegion);
    quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mPopUpMenuScene.addMenuItem(quitMenuItem);

    //build camera 
    this.mPopUpMenuScene.setMenuAnimator(new SlideMenuAnimator());
    this.mPopUpMenuScene.buildAnimations();
    this.mPopUpMenuScene.setBackgroundEnabled(false);
    this.mPopUpMenuScene.setOnMenuItemClickListener(this);

}
}

StartActivity.java

public class StartActivity extends BaseGameActivity {

/*
 * Constants
 */

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

/*
 * Fields
 */

private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mTextureRegion;
private Handler mHandler;

/*
 * Constructors
 */

/*
 * Getter and Setter
 */

/*
 * Method for/from SuperClass/Interfaces
 */

@Override
public Engine onLoadEngine() {
    mHandler = new Handler();
    // put camera
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
            this.mCamera));
}

@Override
public void onLoadResources() {
    this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mBitmapTextureAtlas, this, "gfx/Splashscreen.png", 0, 0);
    this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}

@Override
public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    /* Center the splash on the camera. */
    final int centerX = (CAMERA_WIDTH - this.mTextureRegion
            .getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mTextureRegion
            .getHeight()) / 2;
    final Sprite slash = new Sprite(centerX, centerY,
            this.mTextureRegion);
    scene.attachChild(slash);
    return scene;       
}

@Override
public void onLoadComplete() {      
    mHandler.postDelayed(mLauchTask, 3000);
}




/*
 * Methods
 */

/*
 * Inner and Anonymous Classes
 */
private Runnable mLauchTask = new Runnable() {
    public void run() {
        Intent myIntent = new Intent(StartActivity.this, MainActivity.class);
        StartActivity.this.startActivity(myIntent);
    }
};
}

И я вижу в logcat

05-29 18: 44: 50.338: W / dalvikvm (1049): threadid = 1: поток выходит с необработанным исключением (группа =0x4001d800) 05-29 18:44:50.358: E/AndroidRuntime(1049): ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: main 05-29 18:44:50.358: E/AndroidRuntime(1049): java.lang.NullPointerException 05-29 18:44:50.358: E/AndroidRuntime(1049): на org.anddev.andengine.entity.sprite.Sprite.(Sprite.java:27) 05-29 18:44:50.358: E/AndroidRuntime(1049): в org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem.(SpriteMenuItem.java:29) 05-29 18:44:50.358: E/AndroidRuntime(1049): на com.khoado.v3.MainActivity.createPopUpMenuScene(MainActivity.java:285) 05-29 18:44:50.358: E/AndroidRuntime(1049): на com.khoado.v3.MainActivity.onLoadScene(MainActivity.java:154) 05-29 18:44:50.358: E/AndroidRuntime(1049): в org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:169) 05-29 18:44:50.358: E/AndroidRuntime(1049): в org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85) 05-29 18:44:50.358: E/AndroidRuntime(1049): на com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:1981) 05-29 18:44:50.358: E/AndroidRuntime(1049): в android.view.View.dispatchWindowFocusChanged(View.java:3788) 05-29 18:44:50.358: E/AndroidRuntime(1049): в android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:658) 05-29 18:44:50.358: E/AndroidRuntime(1049): на android.view.ViewRoot.handleMessage(ViewRoot.java:1921) 05-29 18:44:50.358: E/AndroidRuntime(1049): на android.os.Handler.dispatchMessage(Handler.java:99) 05-29 18:44:50.358: E/AndroidRuntime(1049): на android.os.Looper.loop(Looper.java:123) 05-29 18:44:50.358: E/AndroidRuntime(1049): в android.app.ActivityThread.main(ActivityThread.java:4627) 05-29 18:44:50.358: E/AndroidRuntime(1049): в java.lang.reflect.Method.invokeNative(собственный метод) 05-29 18: 44: 50.358: E / AndroidRuntime (1049): в java.lang.reflect.Method.invoke(Method.java:521) 05-29 18:44:50.358: E/AndroidRuntime(1049): в Колорадо m.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-29 18:44:50.358: E/AndroidRuntime(1049): на com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-29: 18:44:50.358: E/AndroidRuntime(1049): at dalvik.system.NativeStart.main(собственный метод)

Почему там ошибка?

1 ответ

Вы получили исключение нулевого указателя в вашем createPopUpMenuScene метод. Номер строки, генерирующей это исключение, - 285. В любом случае, потому что я не могу определить 285-ю строчку, на которые вы разместили строки, поэтому я не могу дать вам четкий и прямой ответ.

По моему мнению, такие проблемы можно легко решить, пройдя по строкам кода путем отладки приложения.

Другие вопросы по тегам