将 Toast 与 AndEngine 一起使用?
Using Toast with AndEngine?
我是 AndEngine 的新手。这是一个优秀而强大的引擎,但是,有一些令人沮丧的事情,例如无法绘制圆圈(顺便说一句,有什么办法可以做到这一点吗?!)
但我担心的是我有一个棋盘游戏,我只是想显示一个 Toast 消息。我把命令 scene.attachChild()
放在 public Scene onCreateScene()
里面,但是当尝试 运行 游戏时,它毁了
public class MainActivity extends SimpleBaseGameActivity {
private static int CAMERA_WIDTH ;
private static int CAMERA_HEIGHT;
// private int numRows= 8;
// private int numColumns= 10;
private Font mFont;
private ITexture mFaceTexture;
private ITextureRegion mFaceTextureRegion;
private ITexture boardTexture;
private ITextureRegion boardTextureRegion;
@Override
public EngineOptions onCreateEngineOptions() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
CAMERA_WIDTH = metrics.widthPixels;
CAMERA_HEIGHT = metrics.heightPixels;
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() throws IOException {
this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 16);
this.mFont.load();
this.boardTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "board.png");
this.boardTextureRegion = TextureRegionFactory.extractFromTexture(this.boardTexture);
this.boardTexture.load();
this.mFaceTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "GraduationIcon.png");
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mFaceTexture);
this.mFaceTexture.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.getBackground().setColor(Color.BLACK);
/* Create the rectangles. */
final Sprite board = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, this.boardTextureRegion, this.getVertexBufferObjectManager());
board.setScale(.5f);
/* Create the sprite and add it to the scene. */
final Sprite face = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
/* draw a line */
float x1=CAMERA_WIDTH/2;
float y1=CAMERA_HEIGHT/2;
float x2=CAMERA_WIDTH *7/8;
float y2=CAMERA_HEIGHT /2;
Random random = new Random(123456789);
float lineWidth=2;
final Line line = new Line(x1+20, y1, x2, y2, lineWidth, this.getVertexBufferObjectManager());
line.setColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
/* draw a text */
final Text text = new Text(x2, y2 , this.mFont, "Week when you are graduated\n (you are 50!)", new TextOptions(HorizontalAlign.CENTER), this.getVertexBufferObjectManager());
/*add graphics to the scene */
scene.attachChild(board);
scene.attachChild(face);
scene.attachChild(line);
scene.attachChild(text);
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
return scene;
}
}
有什么问题吗? :?
这是 logcat
错误
07-14 10:14:22.301: E/AndroidRuntime(21709): FATAL EXCEPTION: Thread-3085
07-14 10:14:22.301: E/AndroidRuntime(21709): Process: com.example.hello, PID: 21709
07-14 10:14:22.301: E/AndroidRuntime(21709): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.os.Handler.<init>(Handler.java:200)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.os.Handler.<init>(Handler.java:114)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.widget.Toast$TN.<init>(Toast.java:344)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.widget.Toast.<init>(Toast.java:100)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.widget.Toast.makeText(Toast.java:258)
07-14 10:14:22.301: E/AndroidRuntime(21709): at com.example.hello.MainActivity.run(MainActivity.java:143)
07-14 10:14:22.301: E/AndroidRuntime(21709): at java.lang.Thread.run(Thread.java:818)
在 UI 线程中尝试 运行 Toast
activity.runOnUiThread(new Runnable(){
@Override
public void run(){
Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
}
}
他们用这个 Toast
代替:
toastOnUiThread("Your message!", Toast.LENGTH_LONG);
我是 AndEngine 的新手。这是一个优秀而强大的引擎,但是,有一些令人沮丧的事情,例如无法绘制圆圈(顺便说一句,有什么办法可以做到这一点吗?!)
但我担心的是我有一个棋盘游戏,我只是想显示一个 Toast 消息。我把命令 scene.attachChild()
放在 public Scene onCreateScene()
里面,但是当尝试 运行 游戏时,它毁了
public class MainActivity extends SimpleBaseGameActivity {
private static int CAMERA_WIDTH ;
private static int CAMERA_HEIGHT;
// private int numRows= 8;
// private int numColumns= 10;
private Font mFont;
private ITexture mFaceTexture;
private ITextureRegion mFaceTextureRegion;
private ITexture boardTexture;
private ITextureRegion boardTextureRegion;
@Override
public EngineOptions onCreateEngineOptions() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
CAMERA_WIDTH = metrics.widthPixels;
CAMERA_HEIGHT = metrics.heightPixels;
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() throws IOException {
this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 16);
this.mFont.load();
this.boardTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "board.png");
this.boardTextureRegion = TextureRegionFactory.extractFromTexture(this.boardTexture);
this.boardTexture.load();
this.mFaceTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "GraduationIcon.png");
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mFaceTexture);
this.mFaceTexture.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.getBackground().setColor(Color.BLACK);
/* Create the rectangles. */
final Sprite board = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, this.boardTextureRegion, this.getVertexBufferObjectManager());
board.setScale(.5f);
/* Create the sprite and add it to the scene. */
final Sprite face = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
/* draw a line */
float x1=CAMERA_WIDTH/2;
float y1=CAMERA_HEIGHT/2;
float x2=CAMERA_WIDTH *7/8;
float y2=CAMERA_HEIGHT /2;
Random random = new Random(123456789);
float lineWidth=2;
final Line line = new Line(x1+20, y1, x2, y2, lineWidth, this.getVertexBufferObjectManager());
line.setColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
/* draw a text */
final Text text = new Text(x2, y2 , this.mFont, "Week when you are graduated\n (you are 50!)", new TextOptions(HorizontalAlign.CENTER), this.getVertexBufferObjectManager());
/*add graphics to the scene */
scene.attachChild(board);
scene.attachChild(face);
scene.attachChild(line);
scene.attachChild(text);
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
return scene;
}
}
有什么问题吗? :?
这是 logcat
错误
07-14 10:14:22.301: E/AndroidRuntime(21709): FATAL EXCEPTION: Thread-3085
07-14 10:14:22.301: E/AndroidRuntime(21709): Process: com.example.hello, PID: 21709
07-14 10:14:22.301: E/AndroidRuntime(21709): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.os.Handler.<init>(Handler.java:200)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.os.Handler.<init>(Handler.java:114)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.widget.Toast$TN.<init>(Toast.java:344)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.widget.Toast.<init>(Toast.java:100)
07-14 10:14:22.301: E/AndroidRuntime(21709): at android.widget.Toast.makeText(Toast.java:258)
07-14 10:14:22.301: E/AndroidRuntime(21709): at com.example.hello.MainActivity.run(MainActivity.java:143)
07-14 10:14:22.301: E/AndroidRuntime(21709): at java.lang.Thread.run(Thread.java:818)
在 UI 线程中尝试 运行 Toast
activity.runOnUiThread(new Runnable(){
@Override
public void run(){
Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
}
}
他们用这个 Toast
代替:
toastOnUiThread("Your message!", Toast.LENGTH_LONG);