Home » Android » Android unable to find my onClick method

Android unable to find my onClick method

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am having an issue where my Button (id i_am_a_problem) which is declared in the fragment’s layout XML, is generating an error when the button is clicked. Android tries to call the onClick method public void calculate(View v) but is unable to find it (despite it being declared in MainActivity). Details of my code are below, and the error is after that. Please help me determine why android is unable to find the onClick method. Thanks.

There is really not anything else going on in this code (fresh new project)

MainActivity.java

package supercali.FRAG.alistic;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) { /*snip - irrelevance*/ }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) { /*snip - irrelevance*/ }

    public void calculcate(View v){
        TextView tv = (TextView)findViewById(R.id.results);
        tv.setText(calculate_value());
    }

    private String calculate_value(){
        return "Abra Kadabra";
    }
}

MainActivity’s layout just contains a fragment

<fragment
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/fragment"
  android:name="supercali.FRAG.alistic.MainActivityFragment" 
  tools:layout="@layout/fragment_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

And the layout for that Fragment is:

<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: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=".MainActivityFragment">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:onClick="calculate"
        android:id="@+id/i_am_a_problem"/>

    <TextView android:text="@string/results_pending" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/results"/>

</LinearLayout>

The problem Button is just there ^^ with id i_am_a_problem

LogCat:

06-18 09:49:41.280  31642-31642/supercali.FRAG.alistic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: supercali.FRAG.alistic, PID: 31642
    java.lang.IllegalStateException: Could not find method calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
            at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4441)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4405)
            at android.view.View.performClick(View.java:5147)
            at android.view.View$PerformClick.run(View.java:21069)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5401)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
How to&Answers:

You misspelled the method.
the XML is looking for calculate but your method is calculcate

Answer:

Your onCLick method is called “calculcate” in the Activity. In the XML it’s “calculate”.

Answer:

I faced the same error but my onClick method was overridden from my base activity class and when I override it with editor help its signature became protected and hence was the problem.

Problem was resolved when I made it public.

Answer:

Write the Method name correctly:

public void calculate(View v){
        TextView tv = (TextView)findViewById(R.id.results);
        tv.setText(calculate_value());
    }

Answer:

Just both spelling are different….

   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:onClick="calculcate"
        android:id="@+id/i_am_a_problem"/>

I just put android:onClick=”calculcate”
While you used word “calculcate”

 public void calculcate(View v){
.....
}

And you used android:onClick="calculate"

Answer:

You misspelled calculate. And if that doesn’t work, try View view instead of View v.

Answer:

Actually, In AndroidManifest file to verify name correct or not. AndroidManifest shown read color line if xml file name and class file name not relate.

Base on the problem, onClickListener can’t find your class files because of wrong name. Error will only show Onclick error. This problem could confuse that you may set wrong onClickListener but actual problem is you set wrong name in xml file and activity class.

Sometime, you will not see error in xml and activity files because of same android name. In this case you can verify easily what was wrong in AndroidManifest file.