0

I'm extremely new to Android and Java but this site has helped me until now, so thanks to you all!

I am building an app that has two tabs, in one tab I have created some buttons and in the corresponding activity I have an OnClickListener. When I run the app it forces close and I get the error:

ERROR/AndroidRuntime(25971): java.lang.IllegalStateException: Could not find a method myClickHandler(View) in the activity class com.test.rate.MainActivity for onClick handler on view class android.widget.Button with id 'CalculateButton'

It's probably really simple but help!

Okay yeah I should have added some code:

The Activity:

public class MetricActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.metriclayout);


    final Button button = (Button) findViewById(R.id.CalculateButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
        }
    });

And the button in the tab layout:

<Button android:layout_height="wrap_content" android:text="Calculate" android:layout_width="wrap_content" android:id="@+id/CalculateButton" android:onClick="myClickHandler" android:layout_gravity="center"></Button>
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Leon
  • 1
  • 1
  • 2

4 Answers4

1

Did you define an onclick method in the xml file for the layout, or are you setting the OnClickListener in the activtity? Or both?

http://developer.android.com/reference/android/view/View.html#attr_android:onClick

It seems like you may have defined the onclick xml attribute to myClickHandler but you never implemented the method.

Zambotron
  • 699
  • 5
  • 14
0

I'm guessing you probably set android:clickable="true" in your xml file then you didn't define an onClickListener in your activity. In your onCreate() method of your activity define an onClickListener for your button:

Button btn = (Button) findViewById(R.id.yourbuttonid);
btn.setOnClickListener(new OnClickListener(){

  public void onClick(View v) {
      //do whatever when your button is clicked
  }

});
0

You don't need that:

android:onClick="myClickHandler"

Clear it and the rest will be fine.

Zappescu
  • 1,429
  • 2
  • 12
  • 25
0
  1. Remove that "onClickHandler" thing from your XML
  2. On your onClick method, create a new Intent with the activity you're trying to display
  3. Add the Activity to your Manifest

Look at the question and answer here: Using Intent in an Android application to show another activity . They address points 2 and 3 respectively.

Community
  • 1
  • 1
Vicente Plata
  • 3,370
  • 1
  • 19
  • 26