如标题所示,我正在尝试在我的 Android 应用程序中创建一个按钮来更改 TextView,但它无法正常工作......这是代码,我希望你们看看出了什么问题。仅供参考,我正在使用连接了 Android 2.3.3 模拟器的 NetBeans,因为稍后我将在我的手机 (2.3.6) 上运行。
主要java:
public class Rooster extends Activity
{
private Button buttonSearch;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
this.buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textRooster = (TextView)findViewById(R.id.textRooster);
textRooster.setText("some text");
}
});
}
那么这是我的main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="42px"
android:layout_weight="1"
android:gravity="center_vertical" >
<EditText
android:id="@+id/editSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="2"
android:gravity="center"
android:hint="Zoekopdracht" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="1"
android:gravity="center"
android:text="Zoek" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="39px"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="@+id/buttonChangeWijzigingen"
android:layout_width="fill_parent"
android:layout_height="37px"
android:gravity="center"
android:text="Zet wijzigingen aan" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="12" >
<TextView
android:id="@+id/textRooster"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Geen rooster opgezocht" />
</LinearLayout>
</LinearLayout>