y43*_*34y 4 android handler surfaceview
我的问题是自定义GameView(扩展SurfaceView)和TextView之间的通信:我想从GameView内部设置TextView的文本.在主要活动我正在使用这个布局文件,它应该解释我的应用程序的结构:
<?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>
Run Code Online (Sandbox Code Playgroud)
我无法在GameView对象中更改TextView的文本,因为无法从另一个接触UI线程.Handler也不起作用,因为我无法给Gamecher的构造函数提供一个处理程序的引用,这是在加载这个xml文件后执行的(读取xml文件的默认构造函数,例如这里我如何在LinearLayout中使用GLSurfaceView和其他视图,如TextView或Button?).你知道我现在应该做什么吗?也许我的演绎错了,所以请告诉我这个.
编辑:我改变了我的xml文件,而不是我现在拥有的GameView:
<LinearLayout
android:id="@+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我还在构造函数的签名中添加了一个参数(第三个):
public GameView(Context context, AttributeSet as, Handler h) { ... }
Run Code Online (Sandbox Code Playgroud)
并在GameplayActivity中更改了我的onCreate:
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);
}
Run Code Online (Sandbox Code Playgroud)
它可以工作,现在我可以设置TextView的文本,但是在单击后退按钮后会抛出另一个异常:"执行暂停未恢复的活动:{org.gk.grApp/org.gk.grApp.MainMenuActivity}".我刚刚开始搜索有关此信息.
首先在活动级别上引用TextView:
TextView txv;
在onCreate中分配此引用:
txv =(TextView)findViewById(R.id.MyTextView);
然后在onCreate之后在Activity中创建一个方法,如下所示:
public void setTextView(final String txt){
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
txv.setText(txt);
}
});
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以从自定义视图中拨打电话:
((MyActivity)getContext()).setTextView(str);
| 归档时间: |
|
| 查看次数: |
2540 次 |
| 最近记录: |