0

我是 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);
}



}

日志猫:

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
4

4 回答 4

3

错误非常明确:

java.lang.IllegalStateException: Could not find a method SendMessage(View) in the activity

替换android:onClick="SendMessage"android:onClick="sendMessage"

于 2013-09-28T09:08:23.417 回答
0

在您的 xml 中将 SendMessage 更改为 sendMessage

于 2013-09-28T09:11:53.057 回答
0

在 Android 清单文件中注册您的活动。

<activity android:name="Your activity name with package" /> 
于 2013-09-28T09:46:59.193 回答
0

    Android.Manifest.xml

Add this to your manifest
Activity android:name="com.newthinktank.myfirstapp.DisplayMessageActivity"

运行::: 你将完成

于 2014-07-02T22:14:20.313 回答