Andnengine在onSceneTouchEvent()时更新textview

时间:2011-12-15 10:38:05

标签: android andengine textview

当用户点击AndEngine中的场景时,有什么方法可以更新TextView。我正在尝试更新我的文本视图,但它给我一个例外。这就是我正在做的事情:

添加textview:

@Override
protected void onSetContentView() {
    final FrameLayout frameLayout = new FrameLayout(this);
    final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);

    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine);
    final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

    hm = getIconNames();

    text = new TextView(this);
    text.setPadding(20, 10, 10, 10);
    text.setText(hm.get(title));
    text.setTextColor(Color.parseColor("#000000"));
    frameLayout.addView(text);

    //Create any other views you want here, and add them to the frameLayout.

    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

我正在尝试这样做:

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    if(this.mPhysicsWorld != null) {
        if(pSceneTouchEvent.isActionDown()) {
            title++;
            text.setText(hm.get(title));
            this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
            return true;
        }
    }
    return false;
}

它给我这个错误:

12-15 12:32:15.584: E/AndroidRuntime(1468): FATAL EXCEPTION: UpdateThread
12-15 12:32:15.584: E/AndroidRuntime(1468): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.view.ViewRoot.checkThread(ViewRoot.java:3165)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:690)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:716)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2624)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.view.View.invalidate(View.java:5341)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.widget.TextView.checkForRelayout(TextView.java:5778)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.widget.TextView.setText(TextView.java:2817)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.widget.TextView.setText(TextView.java:2685)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at android.widget.TextView.setText(TextView.java:2660)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.hardartcore.stefan.kunchev.Game.onSceneTouchEvent(Game.java:245)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:353)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.engine.Engine.onTouchScene(Engine.java:409)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.engine.Engine.onTouchEvent(Engine.java:395)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.input.touch.controller.BaseTouchController$TouchEventRunnablePoolItem.run(BaseTouchController.java:126)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.util.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:47)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.util.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:1)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.util.pool.PoolUpdateHandler.onUpdate(PoolUpdateHandler.java:85)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.input.touch.controller.BaseTouchController.onUpdate(BaseTouchController.java:68)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.engine.Engine.onUpdate(Engine.java:481)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.engine.Engine.onTickUpdate(Engine.java:459)
12-15 12:32:15.584: E/AndroidRuntime(1468):     at org.anddev.andengine.engine.Engine$UpdateThread.run(Engine.java:685)

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您无法从更新线程更新本机视图,您必须在UI线程中执行此操作。 你可以这样做:

@Override   
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {   
    if(this.mPhysicsWorld != null) {   
        if(pSceneTouchEvent.isActionDown()) {   
            title++;   
            text.post(new Runnable() {
                @Override
                public void run() {
                    text.setText(hm.get(title));           
                }
            }; 
            this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());   
            return true;   
        }   
    }   
    return false;   
}   

这样可行,但您应该考虑使用AndEngine实体向用户显示信息,也可以使用ChangeableText类,这非常类似于文本视图,或者使用HUD来添加几个entite一次,将在场景中展示。