我的 android 应用程序中出现错误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'
,它只有一个活动类。当我尝试单击应用程序中的唯一按钮(在模拟器中)时,错误出现在 logcat 中
我在我的活动类中添加了 onClick 方法和一个带有id:button1的按钮。下面是我的代码
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;
}
}
我在 logcat 视图中出现以下错误
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
我将不胜感激这方面的任何帮助。