1

我已经创建并运行了基本的 android MyFirstApp

http://developer.android.com/training/basics/firstapp/starting-activity.html

在 Eclipse 中使用 AVD,我收到以下错误:

"java.lang.IllegalStateException:activity在类中找不到com.example.myfirstapp.MainActivityonClick 处理程序的方法viewsendMesaage (View)android.widget.Button"

方法存在且onClick元素存在:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="@string/edit_message" />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMesaage"/> 

方法调用:

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMesaageActivity.class);
    EditText edit_message= (EditText) findViewById(R.id.edit_message);
    String message = edit_message.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
4

2 回答 2

0
<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/> 

方法调用:

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMesaageActivity.class);
EditText edit_message= (EditText) findViewById(R.id.edit_message);
String message = edit_message.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
于 2012-08-03T14:37:22.790 回答
0

检查额外的空格。我遇到了类似的问题。我没有在布局 xml 文件中将函数名称称为“myOnClick”,而是将其输入错误为“myOnClick”。

于 2013-02-19T23:36:53.930 回答