0

我正在尝试在单击按钮时编辑 TextView ...这是代码的 xml 部分

TextView
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:editable="true">
/TextView>
Button
    android:id="@+id/b1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="50dp"
    android:text="1"
    android:onClick="write" />

单击按钮时,写入功能已被调用...一旦调用此写入功能,我的应用程序就会崩溃...

public void write(){
    TextView text=(TextView)findViewById(R.id.editText1);
    text.setText(Integer.toString(1));
}
4

1 回答 1

4

尝试这样的事情

public void write(View view){ 
TextView text=(TextView)view; // can do like this instead of findViewById
text.setText(Integer.toString(1));
}

正如@Doomsknight 评论的那样

你的方法write需要 View 参数,即使它没有被使用。否则它将找不到您尝试调用的方法。

于 2013-02-24T10:22:19.580 回答