Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unable to find my onClick method

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)
like image 899
Zerp Avatar asked Jun 18 '15 15:06

Zerp


4 Answers

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

like image 149
EE66 Avatar answered Sep 19 '22 18:09

EE66


Your onCLick method is called "calculcate" in the Activity. In the XML it's "calculate".

like image 35
user4989692 Avatar answered Sep 17 '22 18:09

user4989692


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.

like image 30
Deepali Maniyar Avatar answered Sep 19 '22 18:09

Deepali Maniyar


Write the Method name correctly:

public void calculate(View v){
        TextView tv = (TextView)findViewById(R.id.results);
        tv.setText(calculate_value());
    }
like image 38
Setu Kumar Basak Avatar answered Sep 19 '22 18:09

Setu Kumar Basak