Hanzo
Hanzo

Reputation: 1899

Call SimpleBaseGameActivity from Intent

I've a doubt. Can I call a SimpleBaseGameActivity of AndEngine from an Intent?

Intent intent = new Intent(getActivity(), GameActivity.class);  

startActivity(intent);

I've already added the library to my project in project.properties

target=android-19
android.library.reference.1=..\\AndEngine-GLES2
android.library.reference.2=..\\appcompat_v7

When I try to launch activity it fails with this locat error

11-28 12:13:18.896: E/AndroidRuntime(14662): FATAL EXCEPTION: main
11-28 12:13:18.896: E/AndroidRuntime(14662): Process: com.example.consejos, PID: 14662
11-28 12:13:18.896: E/AndroidRuntime(14662): java.lang.NullPointerException: Attempt to invoke virtual method 'void org.andengine.engine.Engine.start()' on a null object reference
11-28 12:13:18.896: E/AndroidRuntime(14662):    at org.andengine.ui.activity.BaseGameActivity.onResumeGame(BaseGameActivity.java:222)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at org.andengine.ui.activity.BaseGameActivity$4.run(BaseGameActivity.java:373)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at android.os.Handler.handleCallback(Handler.java:733)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at android.os.Looper.loop(Looper.java:136)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at android.app.ActivityThread.main(ActivityThread.java:5086)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at java.lang.reflect.Method.invoke(Native Method)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-28 12:13:18.896: E/AndroidRuntime(14662):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

This is my Game Activity code

package com.example.consejos;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class MainActivityEx2 extends SimpleBaseGameActivity {

    private Camera mCamera;
    private static final int WIDTH = 1184;
    private static final int HEIGHT = 720;
    private BitmapTextureAtlas miAtlas;
    private ITextureRegion texturaChar;
    private ITiledTextureRegion texturaAnimada;
    private Sprite charSprite;
    private AnimatedSprite spriteAnimado;

    @Override
    public EngineOptions onCreateEngineOptions() {
        mCamera = new Camera(0,0,WIDTH,HEIGHT);
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
        engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        return engineOptions;
    }

    @Override
    protected void onCreateResources() {

        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        miAtlas = new BitmapTextureAtlas(getTextureManager(), 300, 300, TextureOptions.DEFAULT);
        texturaAnimada = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "walk.png", 0, 0, 3, 4);

        miAtlas.load();     
    }

    @Override
    protected Scene onCreateScene() {
        Scene sceneEjemplo = new Scene();
        spriteAnimado = new AnimatedSprite(400, 400,texturaAnimada, getVertexBufferObjectManager());
        long[] duracionFrame = { 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
        spriteAnimado.animate(duracionFrame, 0, 10, true);
        sceneEjemplo.attachChild(spriteAnimado);

        return sceneEjemplo;
    }

}

An this is my caller activty

package com.example.consejos;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MyPassport extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.mypassport, container, false);

        final ImageView volvsol = (ImageView) view.findViewById(R.id.start);
        volvsol.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivityEx2.class);                   
                startActivity(intent);

            }


        });
        return view;
}
}

Upvotes: 1

Views: 616

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

Try to schedule Engine start upon creation.

 @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions) {
        Engine engine = newEngine(pEngineOptions);
        if(scheduleEngineStart){
            engine.start();
            scheduleEngineStart = !scheduleEngineStart;
        }
        return engine;
    }



 @Override
    public synchronized void onResumeGame() {
        if(mEngine != null) {
            super.onResumeGame();
            scheduleEngineStart = true;
        }
    }

Upvotes: 1

Janpu
Janpu

Reputation: 1

Assuming that you still have this issue, it seems to be the same problem as described here:

https://github.com/nicolasgramlich/AndEngine/issues/7

Essentially if you have a ScreenOrientation.LANDSCAPE_FIXED and omit the android:configChanges="orientation|screenSize" for this activity in the AndroidManifest.xml then this error will occur.

So your manifest would need to have something like the following entry for this activity

<activity android:name="com.example.consejos.MyPassport"
        android:configChanges="orientation|screenSize">

The root cause is a bit obscure from the stack trace. The reason it hasn't been "fixed" is because it's easily aviodable in the manifest (provided you know what the solution is of course).

Upvotes: 0

Related Questions