<?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>
我无法在GameView对象中更改TextView的文本,因为无法从另一个接触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) { ... }
并在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); }
它工作正常,现在我可以设置TextView的文本但是在单击后退按钮后会抛出另一个异常:
“执行暂停未恢复的活动:{org.gk.grApp / org.gk.grApp.MainMenuActivity}”.我刚刚开始搜索有关此信息.
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); } }); }
然后,您可以从自定义视图中拨打电话:
((MyActivity) getContext()).setTextView(str);