2013-09-28 92 views
0

我是新的android和這是我的第一個應用程序,我不知道它爲什麼停止? 我使用的Nexus 4模擬器 我認爲的onClick問題,但不知道爲什麼,當我沒有問題刪除accure不幸的是第一個應用程序已停止android-

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 
+2

你拼寫錯誤'SendMessage',它應該是'sendMessage'的XML。 –

+0

我現在編輯這個,但仍然是相同的問題 –

+0

這不是同一個問題,它是一個不同的問題。從日誌: '由:android.content.ActivityNotFoundException:無法找到顯式活動類{com.newthinktank.myfirstapp/com.newthinktank.myfirstapp.DisplayMessageActivity};你是否在AndroidManifest.xml中聲明瞭這個活動? – Simon

回答

3

錯誤很明顯T:

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

在XML

0

變化SendMessage函數sendMesage。

<activity android:name="Your activity name with package" /> 
0

註冊在Android清單文件中的活動替換android:onClick="SendMessage"android:onClick="sendMessage"

0

    Android.Manifest.xml

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

運行:::你會做

相關問題