0

I am getting an error java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.mysostest.MySOSTestActivity for onClick handler on view class android.widget.Button with id 'button1' in my android app which has only one activity class.The error is showing up in logcat when I try to click the only button in the app(in emulator)

I have added onClick method in my activity class and a Button with id:button1.Here follows my code

package com.example.mysostest;

import android.app.ActionBar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
//import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.TextView;
import android.widget.Toast;
import android.app.*;
//import com.example.sostestproject.*;
import com.tcs.sos.api.Connection;
import com.tcs.sos.api.Operations;
import com.tcs.sos.api.OperationsEnum;
import com.tcs.sos.api.OperationsFactory;
import com.tcs.sos.api.operations.InsertObservationRequest;
import com.tcs.sos.api.operations.InsertObservationResponse;
import com.tcs.sos.api.operations.Location;
import com.tcs.sos.api.operations.Observation;
import com.tcs.sos.api.operations.RegisterSensorRequest;
import com.tcs.sos.api.operations.RegisterSensorResponse;
import com.tcs.sos.api.operations.Sensor;
import com.tcs.sos.api.operations.SensorMLOutput;
import com.tcs.sos.api.operations.exceptions.SOSException;
import com.tcs.sos.api.operations.exceptions.ValidationException;;

public class MySOSTestActivity extends Activity {

    final Context context=this;
    public static final String TAG="Just to create LOG";

    static String SOSBaseURL="http://192.168.161.52:8080/52nSOSv3.2.1/sos";
    static String API_KEY = "65bda03805a7f0186ef44687682d108f";
    static String APP_KEY = "e9f718e06f2872f02283b83a4b499ee4";

    private EditText text1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sostest);
        text1=(EditText)findViewById(R.id.editText1);

         Button button=(Button)findViewById(R.id.button1);
         final String inputString=text1.getText().toString();
            if(text1.getText().length()==0){
            Toast.makeText(this, "You have enetered blank ", Toast.LENGTH_LONG).show();
            return;
            }
         button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                try{
                Connection.sos = SOSBaseURL;
                RegisterSensorRequest r = null;
                String ECG_Offering = "Sample_Data_testing_FromAndroidApp";
                r = new RegisterSensorRequest(Sensor.newSensor(inputString));
                r.add("heart_rate_value", ECG_Offering).Quantity();
                r.add("heart_rate_behaviour", ECG_Offering).Text(); //Category Type
                r.add("ecg", ECG_Offering).Text(); //Category Type
                r.add("patientId", ECG_Offering).Text(); //New field to accommodate location
                r.add("Location", ECG_Offering).Location();

                r.setApiKey(API_KEY);
                r.setAppKey(APP_KEY);
                RegisterSensorResponse response = (RegisterSensorResponse)OperationsFactory.get(OperationsEnum.RegisterSensor).execute(r);

                if(response.isSuccess()) {
                    System.out.println("Generating API KEY....");
                    System.out.println("The response is"+response.toString());
                    Log.e(TAG,"Connected to SOS");

                AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(context);
                alertDialogBuilder.setMessage("Connected to SOS")
                                  .setCancelable(false)
                                  .setPositiveButton("OK",new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        dialog.cancel();
                                    }
                                });
                alertDialogBuilder.create();
                alertDialogBuilder.show();
                }
                }catch(ClassNotFoundException ex){
                    ex.printStackTrace();
                }
                catch(IllegalAccessException ex){
                    ex.printStackTrace();
                }
                catch(InstantiationException ex){
                    ex.printStackTrace();
                }
                catch(ValidationException ex){
                    ex.printStackTrace();
                }
                catch(SOSException ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_my_sostest, menu);
        return true;
    }

}

I am having following error in the logcat view

02-14 09:47:49.476: E/AndroidRuntime(839): FATAL EXCEPTION: main
02-14 09:47:49.476: E/AndroidRuntime(839): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.mysostest.MySOSTestActivity for onClick handler on view class android.widget.Button with id 'button1'
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.view.View$1.onClick(View.java:3584)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.view.View.performClick(View.java:4202)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.view.View$PerformClick.run(View.java:17340)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.os.Handler.handleCallback(Handler.java:725)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.os.Looper.loop(Looper.java:137)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-14 09:47:49.476: E/AndroidRuntime(839):  at java.lang.reflect.Method.invokeNative(Native Method)
02-14 09:47:49.476: E/AndroidRuntime(839):  at java.lang.reflect.Method.invoke(Method.java:511)
02-14 09:47:49.476: E/AndroidRuntime(839):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-14 09:47:49.476: E/AndroidRuntime(839):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 09:47:49.476: E/AndroidRuntime(839):  at dalvik.system.NativeStart.main(Native Method)
02-14 09:47:49.476: E/AndroidRuntime(839): Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
02-14 09:47:49.476: E/AndroidRuntime(839):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
02-14 09:47:49.476: E/AndroidRuntime(839):  at java.lang.Class.getMethod(Class.java:915)
02-14 09:47:49.476: E/AndroidRuntime(839):  at android.view.View$1.onClick(View.java:3577)
02-14 09:47:49.476: E/AndroidRuntime(839):  ... 11 more
02-14 09:48:33.115: I/Process(839): Sending signal. PID: 839 SIG: 9

I would appreciate any help on this.

Samrat
  • 161
  • 2
  • 16

1 Answers1

1

You are getting error as you have specified onClick property in xml file, too and you are also setting listener for same button in java code, too.

Just remove onClick from xml file. Or keep onClick only and remove listener from java file.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124