[英]App crashes when set value to textview
I have made a simple activity with a textview, edittext and button. 我使用textview,edittext和button进行了一个简单的活动。 and I am trying to display value of edittext in textview when button is presses.
并且我试图在按下按钮时在textview中显示edittext的值。
public void click() {
EditText text1 = (EditText) findViewById(R.id.Text1);
TextView text2 = (TextView) findViewById(R.id.Text2);
text2.setText("text");
};
}
XML XML格式
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="click" />
</LinearLayout>
when I press button app crashes. 当我按下按钮时应用崩溃。
Since you are using android:onClick="click"
, android is expecting to find at runtime a method with the following signature: public void click(View view)
. 由于您使用的是
android:onClick="click"
,因此android希望在运行时找到具有以下签名的方法: public void click(View view)
。
change 更改
public void click() {
with 与
public void click(View view) {
where view
is the view's object you clicked on view
是您单击的视图对象
You are using wrong signature. 您使用的签名错误。 You need to add param
您需要添加参数
View view
So, calling of method should look like 因此,方法的调用应类似于
public void click(View view) {//your code here }
Also call method click(View view) in MyActivity class. 还要在MyActivity类中调用方法click(View view)。
Look at Android View : http://developer.android.com/reference/android/view/View.html 查看Android View: http : //developer.android.com/reference/android/view/View.html
You need to pass a View in the method to update or play with it. 您需要在方法中传递一个View来进行更新或使用。 Without which the application will crash.
否则,应用程序将崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.