基本MyFirstApp中的IllegalStateException

时间:2012-08-03 14:32:37

标签: android illegalstateexception

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

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

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

"java.lang.IllegalStateException:无法在activitycom.example.myfirstapp.MainActivity上为viewandroid.widget.Button"

上的onClick处理程序找到方法sendMesaage(View)

该方法存在且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);
}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

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