为什么我的应用程序崩溃?

时间:2013-02-24 10:20:53

标签: android text textview android-edittext edit

我正在尝试在单击按钮时编辑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));
}

1 个答案:

答案 0 :(得分: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参数,即使它未被使用。否则它不会找到你想要调用的方法。