我的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
我将不胜感激。
答案 0 :(得分:1)
您在xml文件中指定了onClick
属性时收到错误,并且您也在java代码中为同一个按钮设置监听器。
只需从xml文件中删除onClick
即可。或者仅保留onClick
并从java文件中删除侦听器。