2015-07-20 44 views
0

我使用textview,edittext和按钮进行了简单的活动。 我试图在按钮按下时在textview中显示edittext的值。将值设置为textview时,应用程序崩溃

public void click() { 
    EditText text1 = (EditText) findViewById(R.id.Text1); 
    TextView text2 = (TextView) findViewById(R.id.Text2);  
     text2.setText("text"); 
     }; 
} 

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> 

当我按下按钮的应用程序崩溃

回答

1

由于您使用的是android:onClick="click",因此android期望在运行时查找具有以下签名的方法:public void click(View view)

变化

public void click() { 

public void click(View view) { 

其中view是你

+0

谢谢你,先生它的工作 –

+0

不用客气, – Blackbelt

+0

@Purushotamrawat如果这个答案已经成功地满足你的需求,请检查问题声誉下面的回答标志(左边)。 – Bonatti

0

点击你正在使用错误的签名视图的对象。您需要添加PARAM

View view 

所以,方法的调用应该看起来像

public void click(View view) {//your code here } 

而且调用MyActivity类方法点击(查看视图)。

相关问题