【问题标题】:Why does my app crash?为什么我的应用程序崩溃?
【发布时间】:2013-02-24 10:20:53
【问题描述】:

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

【问题讨论】:

    标签: android text textview android-edittext edit


    【解决方案1】:

    试试这样的

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

    正如@Doomsknight 评论的那样

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

    【讨论】:

    • 是的。它需要 View 参数,即使它没有被使用。否则它将找不到您尝试调用的方法。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2014-09-20
    • 2010-09-20
    • 2015-12-26
    • 2019-01-18
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多