我很怀疑我可以从Intent调用AndEngine的SimpleBaseGameActivity吗?
Intent intent = new Intent(getActivity(), GameActivity.class);
startActivity(intent);
我已经将该库添加到了
project.properties
中的项目中target=android-19
android.library.reference.1=..\\AndEngine-GLES2
android.library.reference.2=..\\appcompat_v7
当我尝试启动活动时,它失败并显示此locat错误
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)
这是我的游戏活动代码
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;
}
}
这是我的来电者活动
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;
}
}
最佳答案
尝试安排引擎在创建时启动。
@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;
}
}