我的问题是自定义GameView(扩展SurfaceView)和TextView之间的通信:我想从GameView内部设置TextView的文本。 在主要活动中我使用这个布局文件,这里需要说明一下我的应用程序的结构:自定义SurfaceView和默认Android视图之间的通信
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
>
<TextView
android:id="@+id/scoreTV"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="score: 0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:layout_alignParentRight="true" />
</RelativeLayout>
<org.gk.grApp.GameView
android:id="@+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
我不能改变的TextView的文字在我的GameView对象,因为这是不可能从另一个触摸UI线程。 处理程序不起作用,因为我无法给出处理程序对GameView的构造函数的引用,这是在加载此xml文件后执行的(有关xml文件的默认构造函数(例如,这里为How can I use GLSurfaceView in a LinearLayout together with other Views, such as TextView or Button?)的读取。 你知道我现在应该做什么吗?也许我的演绎是错误的,所以请告诉我这件事。
编辑:我改变了我的XML文件,而不是GameView的我现在有:
<LinearLayout
android:id="@+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
而且我添加了一个参数(第三)为构造函数的签名:
public GameView(Context context, AttributeSet as, Handler h) { ... }
,并改变了我的onCreate中GameplayActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
LinearLayout ll = (LinearLayout)findViewById(R.id.gameplayScreen);
GV = new GameView(this, null, scoreHandler);
ll.addView(GV);
}
它的工作原理,现在我可以设置TextView的文本,但点击后后退按钮会引发另一个异常: “执行暂停的未恢复活动:{org.gk.grApp/org.gk.grApp.MainMenuActivity}”。我刚开始搜索有关这方面的信息。
它不起作用。同样的效果:“只有创建视图层次结构的oryginal线程才能触及它”。 :/ – y434y 2011-12-31 20:00:18
检查编辑答案 – Lumis 2012-01-01 14:16:27
Lumis,感谢您的提示,它的工作完美! :] – y434y 2012-01-02 17:45:50