当我在手机上运行我的应用程序时,出现错误:
不幸的是,当我按下切换按钮时,应用程序 gemdata 已停止
我不知道出了什么问题,有人可以帮忙吗?
感谢 Wesley,我发现问题发生了,因为我在我的 xml 文件中设置了 Onclick 属性
我的代码如下所示:
package dk.SimonPedersen.gemdata2;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
public static final String GEMFFS = "dk.SimonPedersen.GEMFFS";
protected static final boolean Sex = false;
SharedPreferences ada;
SharedPreferences.Editor editor;
Button btnGem;
ToggleButton tbKon;
EditText etVaegt;
Double sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ada = getSharedPreferences(GEMFFS, 0);
editor = ada.edit();
btnGem = (Button) findViewById (R.id.btnGem);
tbKon = (ToggleButton) findViewById (R.id.tbKon);
etVaegt = (EditText) findViewById (R.id.etVaegt);
tbKon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
sex=0.7d;
} else {
sex=0.6d;
}
}
});
}
}
这是我在 logcat 中遇到的错误:
11-13 14:59:07.635: D/AndroidRuntime(12858): Shutting down VM
11-13 14:59:07.635: W/dalvikvm(12858): threadid=1: thread exiting with uncaught exception (group=0x414e4700)
11-13 14:59:07.635: E/AndroidRuntime(12858): FATAL EXCEPTION: main
11-13 14:59:07.635: E/AndroidRuntime(12858): java.lang.IllegalStateException: Could not find a method onToggleClicked(View) in the activity class dk.SimonPedersen.gemdata2.MainActivity for onClick handler on view class android.widget.ToggleButton with id 'tbKon'
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.view.View$1.onClick(View.java:3620)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.view.View.performClick(View.java:4240)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.view.View$PerformClick.run(View.java:17722)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.os.Handler.handleCallback(Handler.java:730)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.os.Looper.loop(Looper.java:137)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.app.ActivityThread.main(ActivityThread.java:5303)
11-13 14:59:07.635: E/AndroidRuntime(12858): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 14:59:07.635: E/AndroidRuntime(12858): at java.lang.reflect.Method.invoke(Method.java:525)
11-13 14:59:07.635: E/AndroidRuntime(12858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
11-13 14:59:07.635: E/AndroidRuntime(12858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
11-13 14:59:07.635: E/AndroidRuntime(12858): at dalvik.system.NativeStart.main(Native Method)
11-13 14:59:07.635: E/AndroidRuntime(12858): Caused by: java.lang.NoSuchMethodException: onToggleClicked [class android.view.View]
11-13 14:59:07.635: E/AndroidRuntime(12858): at java.lang.Class.getConstructorOrMethod(Class.java:423)
11-13 14:59:07.635: E/AndroidRuntime(12858): at java.lang.Class.getMethod(Class.java:787)
11-13 14:59:07.635: E/AndroidRuntime(12858): at android.view.View$1.onClick(View.java:3613)
11-13 14:59:07.635: E/AndroidRuntime(12858): ... 12 more