Search code examples
javaandroidxmlillegalstateexception

Could not find a method [...](View) in the activity class [...] for onClick handler on


When I press the button in fragment_main.xml, this error comes:
java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on view class android.widget.Button

How can I resolve this error?

Full error

02-09 21:01:39.271  13128-13128/com.example.alexander.mobileapp02 D/AndroidRuntime﹕ Shutting down VM
02-09 21:01:39.271  13128-13128/com.example.alexander.mobileapp02 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bd4c08)
02-09 21:01:39.281  13128-13128/com.example.alexander.mobileapp02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.alexander.mobileapp02, PID: 13128
    java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity for onClick handler on view class android.widget.Button
            at android.view.View$1.onClick(View.java:3970)
            at android.view.View.performClick(View.java:4654)
            at android.view.View$PerformClick.run(View.java:19438)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
            at java.lang.Class.getConstructorOrMethod(Class.java:472)
            at java.lang.Class.getMethod(Class.java:857)
            at android.view.View$1.onClick(View.java:3963)
            at android.view.View.performClick(View.java:4654)
            at android.view.View$PerformClick.run(View.java:19438)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
02-09 21:01:46.121  13128-13128/com.example.alexander.mobileapp02 I/Process﹕ Sending signal. PID: 13128 SIG: 9

fragment_main.xml

Shows the button and where you can enter your name

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment"
    android:background="#A9F5F2">

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

activity_main.xml

Nothing really (just an empty page with "Hello world!")

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />

MainActivity.java

package com.example.alexander.mobileapp02;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}

Solution

  • The error is clear:

    Could not find a method sendMessage(View) in the activity class com.example.alexander.mobileapp02.MainActivity

    When you declare in XML a line like this:

    android:onClick="sendMessage"
    

    You need to create the method that gets executed in your MainActivity, it takes the View that called it as a parameter.

    public void sendMessage(View myView)
    {
        //Your code here
    }
    

    Add this in your MainActivity and fill out the code that does the sendMessage.