我正在尝试学习Android,我决定尝试使用andengine 的简单游戏。这个想法是在主菜单中你可以拍照,从外部加载图片,或者只使用默认值。当它收到位图时,我可以很好地加载游戏。但是我检查图片是否存在以便它可以决定加载默认图片的方式一定是错误的。望各位帮忙。提前致谢。这是发送意图的代码块。
Intent launchIntent = new Intent(GameActivity.this, Game.class);
startActivity(launchIntent);
这是获得意图的代码
public class Game extends SimpleBaseGameActivity {
private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;
private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
private Sprite mTower1, mTower2, mTower3;
private Stack mStack1, mStack2, mStack3;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
protected void onCreateResources() {
try {
// 1 - Set up bitmap textures
ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BitmapImage");
@Override
public InputStream open() throws IOException {
if (bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, stream);
InputStream is = new ByteArrayInputStream(stream.toByteArray());
return is;
}
else {
return getAssets().open("gfx/tower.png");
}
}
});
我相当确定问题出在 OnCreateResources 的某个地方。再次感谢!
此外,如果它有所作为(我严重怀疑它)。意图通过清单中的 onClick= 启动
日志猫:
08-01 02:25:41.933: E/AndroidRuntime(825): FATAL EXCEPTION: main
08-01 02:25:41.933: E/AndroidRuntime(825): java.lang.NullPointerException
08-01 02:25:41.933: E/AndroidRuntime(825): at org.andengine.ui.activity.BaseGameActivity.onResumeGame(BaseGameActivity.java:222)
08-01 02:25:41.933: E/AndroidRuntime(825): at org.andengine.ui.activity.BaseGameActivity$4.run(BaseGameActivity.java:373)
08-01 02:25:41.933: E/AndroidRuntime(825): at android.os.Handler.handleCallback(Handler.java:725)
08-01 02:25:41.933: E/AndroidRuntime(825): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 02:25:41.933: E/AndroidRuntime(825): at android.os.Looper.loop(Looper.java:137)
08-01 02:25:41.933: E/AndroidRuntime(825): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 02:25:41.933: E/AndroidRuntime(825): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 02:25:41.933: E/AndroidRuntime(825): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 02:25:41.933: E/AndroidRuntime(825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 02:25:41.933: E/AndroidRuntime(825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 02:25:41.933: E/AndroidRuntime(825): at dalvik.system.NativeStart.main(Native Method)