我正在通过Andengine为Android平台开发游戏。我创建了一个LevelEditorScene,我想在其上使用Toast消息,如:
Toast.makeText(activity, " Upload Started ...", Toast.LENGTH_SHORT).show();
但它会造成错误。这是logCat: http://s9.postimg.org/jfqwsvx7j/Captu1re.png
问题是什么,你可以解决吗?
以下是我的全部代码:
LevelEditorScene.java
@Override
public void createScene()
{
//ekrana eleman eklemek için
setOnSceneTouchListener(this);
Rectangle save = new Rectangle(100, 400, 80, 50, vbom){
@Override
public boolean onAreaTouched(TouchEvent pTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
{
Toast.makeText(activity, " Upload Started ...", Toast.LENGTH_SHORT).show();
/* getAllObjectsData();
createXmlFile(x, y, distanceordirection, type);
*/
}
return true;
}
};
save.setColor(1.0f,0.4f,0.5f);
registerTouchArea(save);
attachChild(save);
}
SceneManager.java
public void loadEditorScene(final Engine mEngine)
{
setScene(loadingScene);
ResourcesManager.getInstance().unloadMenuTextures();
mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
mEngine.unregisterUpdateHandler(pTimerHandler);
ResourcesManager.getInstance().loadGameResources();
editorScene = new LevelEditorScene();
setScene(editorScene);
}
}));
}
GameActivity.java
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
{
mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
mEngine.unregisterUpdateHandler(pTimerHandler);
SceneManager.getInstance().createMenuScene();
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
答案 0 :(得分:1)
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity.this, "message", Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
如果你在静态功能中使用Toast,它必须不起作用
你可以在oncreate函数之前添加这个:
static Activity thisActivity = null;
并在oncreate函数中:
thisActivity = this;
然后你可以使用Toast:
Toast.makeText(thisActivity , "message", Toast.LENGTH_SHORT).show();