我正在尝试在 android 中使用 andengine.And 制作简单的河内塔游戏,我遇到了异常
I am trying to make simple game Tower of Hanoi in android with andengine.And i am getting below Exception
package com.example.testnew;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;
import java.io.IOException;
import java.io.InputStream;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends SimpleBaseGameActivity {
private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;
private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
ITexture backgroundTexture,ring1,ring2,ring3,towerTexture;
@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() throws IOException {
try{
ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException
{
//AssetManager Class:-getAsset();
//Provides access to an application's raw asset files
// for the way most applications will want to retrieve their resource data.
return getAssets().open("gfx/background.png");
}
});
ITexture towerTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/tower.png");
}
});
ITexture ring1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ring1.png");
}
});
ITexture ring2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ring2.png");
}
});
ITexture ring3 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ring3.png");
}
});
backgroundTexture.load();
towerTexture.load();
ring1.load();
ring2.load();
ring3.load();
} catch (IOException e) {
Debug.e(e);
}
this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
this.mRing1 = TextureRegionFactory.extractFromTexture(ring1);
this.mRing2 = TextureRegionFactory.extractFromTexture(ring2);
mBackgroundTextureRegion.setTextureSize(1600, 1000);
}
@Override
protected Scene onCreateScene() {
final Scene scene = new Scene();
Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
scene.attachChild(backgroundSprite);
return scene;
}
}
我的异常:
07-03 13:04:23.904: E/AndEngine(7060): MainActivity.onCreateGame failed. @(Thread: 'GLThread 216')
07-03 13:04:23.904: E/AndEngine(7060): java.lang.NullPointerException
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.opengl.texture.region.TextureRegionFactory.extractFromTexture(TextureRegionFactory.java:50)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.opengl.texture.region.TextureRegionFactory.extractFromTexture(TextureRegionFactory.java:42)
07-03 13:04:23.904: E/AndEngine(7060): at com.example.testnew.MainActivity.onCreateResources(MainActivity.java:144)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.ui.activity.SimpleBaseGameActivity.onCreateResources(SimpleBaseGameActivity.java:43)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:183)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:112)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80)
07-03 13:04:23.904: E/AndEngine(7060): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1494)
07-03 13:04:23.904: E/AndEngine(7060): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
您的问题可能是您捕获并记录了任何异常,然后继续您的 onCreateResources
方法,这意味着您的一些变量(backgroundTexture
、towerTexture
、ring1
或 ring2
- 以第 43 行中的那个为准)保持未初始化状态,导致 NullPointerException
.
而不是 Debug.e(e)
,重新将异常作为初学者的 RuntimeException
抛出,这样您的代码将编译,并且您的代码将因问题的真正原因而崩溃 - 直到您修复它:
} catch (IOException e) {
throw new RuntimeException(e);
}
一般来说,捕获并记录异常然后继续运行是非常糟糕的做法,因为您在不一致的状态下继续运行程序。我总是首先将异常重新抛出为未经检查的异常 (RuntimeException
),直到我的代码正常工作,然后我才弄清楚我想如何处理特定的异常。
package com.example.testnew;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;
import java.io.IOException;
import java.io.InputStream;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends SimpleBaseGameActivity {
private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;
private ITextureRegion mBackgroundTextureRegion, mTowerTextureRegion, mRing1, mRing2, mRing3;
ITexture backgroundTexture,ring1,ring2,ring3,towerTexture;
@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() throws IOException {
try{
ITexture backgroundTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException
{
//AssetManager Class:-getAsset();
//Provides access to an application's raw asset files
// for the way most applications will want to retrieve their resource data.
return getAssets().open("gfx/background.png");
}
});
ITexture towerTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/tower.png");
}
});
ITexture ring1 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ring1.png");
}
});
ITexture ring2 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ring2.png");
}
});
ITexture ring3 = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ring3.png");
}
});
backgroundTexture.load();
towerTexture.load();
ring1.load();
ring2.load();
ring3.load();
} catch (IOException e) {
Debug.e(e);
}
this.mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);
this.mTowerTextureRegion = TextureRegionFactory.extractFromTexture(towerTexture);
this.mRing1 = TextureRegionFactory.extractFromTexture(ring1);
this.mRing2 = TextureRegionFactory.extractFromTexture(ring2);
mBackgroundTextureRegion.setTextureSize(1600, 1000);
}
@Override
protected Scene onCreateScene() {
final Scene scene = new Scene();
Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
scene.attachChild(backgroundSprite);
return scene;
}
}
我的异常:
07-03 13:04:23.904: E/AndEngine(7060): MainActivity.onCreateGame failed. @(Thread: 'GLThread 216')
07-03 13:04:23.904: E/AndEngine(7060): java.lang.NullPointerException
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.opengl.texture.region.TextureRegionFactory.extractFromTexture(TextureRegionFactory.java:50)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.opengl.texture.region.TextureRegionFactory.extractFromTexture(TextureRegionFactory.java:42)
07-03 13:04:23.904: E/AndEngine(7060): at com.example.testnew.MainActivity.onCreateResources(MainActivity.java:144)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.ui.activity.SimpleBaseGameActivity.onCreateResources(SimpleBaseGameActivity.java:43)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:183)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:112)
07-03 13:04:23.904: E/AndEngine(7060): at org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80)
07-03 13:04:23.904: E/AndEngine(7060): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1494)
07-03 13:04:23.904: E/AndEngine(7060): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
您的问题可能是您捕获并记录了任何异常,然后继续您的 onCreateResources
方法,这意味着您的一些变量(backgroundTexture
、towerTexture
、ring1
或 ring2
- 以第 43 行中的那个为准)保持未初始化状态,导致 NullPointerException
.
而不是 Debug.e(e)
,重新将异常作为初学者的 RuntimeException
抛出,这样您的代码将编译,并且您的代码将因问题的真正原因而崩溃 - 直到您修复它:
} catch (IOException e) {
throw new RuntimeException(e);
}
一般来说,捕获并记录异常然后继续运行是非常糟糕的做法,因为您在不一致的状态下继续运行程序。我总是首先将异常重新抛出为未经检查的异常 (RuntimeException
),直到我的代码正常工作,然后我才弄清楚我想如何处理特定的异常。