我是android的新手,这是我的第一个应用程序,我不知道它为什么停止? 我使用Nexus 4模拟器 我认为onClick中的问题,但不知道为什么当我删除没有问题确认
activity_main.xml中
<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"
android:orientation="horizontal"
>
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
MainActivity.java
package com.newthinktank.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE ="com.newthanktank.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
logcat的:
09-28 11:23:13.701: E/AndroidRuntime(1520): FATAL EXCEPTION: main
09-28 11:23:13.701: E/AndroidRuntime(1520): java.lang.IllegalStateException: Could not execute method of the activity
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.view.View$1.onClick(View.java:3591)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.view.View.performClick(View.java:4084)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.view.View$PerformClick.run(View.java:16966)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.os.Handler.handleCallback(Handler.java:615)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.os.Looper.loop(Looper.java:137)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-28 11:23:13.701: E/AndroidRuntime(1520): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 11:23:13.701: E/AndroidRuntime(1520): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 11:23:13.701: E/AndroidRuntime(1520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-28 11:23:13.701: E/AndroidRuntime(1520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 11:23:13.701: E/AndroidRuntime(1520): at dalvik.system.NativeStart.main(Native Method)
09-28 11:23:13.701: E/AndroidRuntime(1520): Caused by: java.lang.reflect.InvocationTargetException
09-28 11:23:13.701: E/AndroidRuntime(1520): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 11:23:13.701: E/AndroidRuntime(1520): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.view.View$1.onClick(View.java:3586)
09-28 11:23:13.701: E/AndroidRuntime(1520): ... 11 more
09-28 11:23:13.701: E/AndroidRuntime(1520): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.newthinktank.myfirstapp/com.newthinktank.myfirstapp.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml?
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.Activity.startActivityForResult(Activity.java:3351)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.Activity.startActivityForResult(Activity.java:3312)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.Activity.startActivity(Activity.java:3522)
09-28 11:23:13.701: E/AndroidRuntime(1520): at android.app.Activity.startActivity(Activity.java:3490)
09-28 11:23:13.701: E/AndroidRuntime(1520): at com.newthinktank.myfirstapp.MainActivity.sendMessage(MainActivity.java:53)
09-28 11:23:13.701: E/AndroidRuntime(1520): ... 14 more
答案 0 :(得分:3)
错误非常明确:
java.lang.IllegalStateException: Could not find a method SendMessage(View) in the activity
将android:onClick="SendMessage"
替换为android:onClick="sendMessage"
答案 1 :(得分:0)
将SendMessage更改为xml中的sendMesage
答案 2 :(得分:0)
在Android Manifest文件中注册您的活动。
<activity android:name="Your activity name with package" />
答案 3 :(得分:0)
Add this to your manifest
Activity android:name="com.newthinktank.myfirstapp.DisplayMessageActivity"