2013-06-18 67 views
0

Java代碼:java.lang.IllegalStateException:無法找到的方法的sendMessage(查看)

package com.example.myfirstapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.EditText; 

public class MainActivity<View> extends Activity { 
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    /** Called when the user clicks the Send button */ 
    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); 

    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<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> 

錯誤消息:

06-18 15:19:12.493: E/AndroidRuntime(799): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button 

我真的不知道這個原因,我檢查方法名稱的情況,沒關係。 誰能幫幫我?

+0

類聲明必須公共類MainActivity擴展活動{}而不是公共類MainActivity 延伸活動{} –

回答

0

刪除<View>。還要將Editext的初始化移動到oncreate()。不需要按鈕點擊初始化editext evey時間。

public class MainActivity extends Activity { 
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 
private EdiText editext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editText = (EditText) findViewById(R.id.edit_message); 
} 
.... 
+0

刪除,請解釋的情況詳細信息 – sevenguin

+0

MainActivity是您的班級名稱。你的課程擴展了Activity。您將佈局的內容設置爲活動。爲什麼你需要這個MainActivity 。它不是類型視圖 – Raghunandan

+0

得到它,非常感謝! – sevenguin

1

必須聲明你的活動爲:

public class MainActivity extends Activity { 
} 

,你也需要聲明你的按鈕在你onCreate()方法象下面這樣:

Button button_send= (Button)findViewById(R.id.your_Button_Id); 

更新:

而且你必須在你的清單中爲你的按鈕指定一個id,並在上面引用它。

+0

不需要ID和按鈕初始化 – Raghunandan

+0

啊,這是正確的,感謝 – sevenguin

相關問題