我已经创建并运行了基本的Android MyFirstApp
http://developer.android.com/training/basics/firstapp/starting-activity.html
在eclipse中使用AVD,我收到以下错误:
"java.lang.IllegalStateException:
无法在activity
类com.example.myfirstapp.MainActivity
上为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);
}
答案 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'。