sevenguin
sevenguin

Reputation: 23

java.lang.IllegalStateException: Could not find a method sendMessage(View)

java code:

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>

the error message:

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

I really do not know the reason about that,and i check the case about the method name,that's ok. Who can help me?

Upvotes: 0

Views: 1880

Answers (2)

Husam A. Al-ahmadi
Husam A. Al-ahmadi

Reputation: 2056

you have to declare your activity as :

public class MainActivity extends Activity {
}

and also you need to declare your button in your onCreate() method like below:

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

update:

Also you have to assign an id for your button in your manifest and refer to it above.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Remove <View>. Also move your initialization of Editext to oncreate(). Need not initialize editext evey time on button click.

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); 
} 
.... 

Upvotes: 0

Related Questions