无法执行活动错误的方法

时间:2014-07-08 23:09:31

标签: java android android-activity checkbox onclick

在我的应用程序中,我有一个复选框,单击该复选框可打开一个新活动。但是,现在我收到标题中提到的错误。我觉得我有这个方法,但我不确定我是否正确使用它。复选框应打开的活动是MajorsList。

public class MainActivity extends Activity {

public static double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
CheckBox checkbox;

public void onCheckBoxClicked(View view){
boolean checked = ((CheckBox) checkbox).isChecked();

if(checked){
    Intent myIntent = new Intent(MainActivity.this, MajorsList.class);
    MainActivity.this.startActivity(myIntent);
}
} 
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gpa = (EditText) findViewById(R.id.gpa);
sat = (EditText) findViewById(R.id.sat);
act = (EditText) findViewById(R.id.act);
checkbox = (CheckBox) findViewById(R.id.checkBox);

score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);

calc.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        String gpaString = gpa.getText().toString();
        if (gpaString.equals("")) {
            gpaString = "0";
        }
        double gpaDouble = Double.parseDouble(gpaString);

        String satString = sat.getText().toString();
        if (satString.equals("")) {
            satString = "0";
        }
        int satInt = Integer.parseInt(satString);

        String actString = act.getText().toString();
        if (actString.equals("")) {
            actString = "0";
        }
        int actInt = Integer.parseInt(actString);
        if (actInt / 36.0 < satInt / 2400.0) {
            scoreDouble = (0.6 * gpaDouble * 25)
                    + (0.4 * ((double) satInt / 2400.0) * 100.0);
        } else {
            scoreDouble = (0.6 * gpaDouble * 25)
                    + (0.4 * ((double) actInt / 36.0) * 100.0);
        }

        Intent myIntent = new Intent(MainActivity.this, CollegeList.class);
        MainActivity.this.startActivity(myIntent);

        }

    }
);
}

}

logcat的

07-08 19:35:31.168: E/AndroidRuntime(2995): FATAL EXCEPTION: main
07-08 19:35:31.168: E/AndroidRuntime(2995): java.lang.IllegalStateException: Could not     execute method of the activity
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.view.View$1.onClick(View.java:3597)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at  android.view.View.performClick(View.java:4202)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.widget.CompoundButton.performClick(CompoundButton.java:100)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.view.View$PerformClick.run(View.java:17340)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.os.Handler.handleCallback(Handler.java:725)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.os.Looper.loop(Looper.java:137)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at dalvik.system.NativeStart.main(Native Method)
07-08 19:35:31.168: E/AndroidRuntime(2995): Caused by: java.lang.reflect.InvocationTargetException
07-08 19:35:31.168: E/AndroidRuntime(2995):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 19:35:31.168: E/AndroidRuntime(2995):     at android.view.View$1.onClick(View.java:3592)
07-08 19:35:31.168: E/AndroidRuntime(2995):     ... 12 more

复选框的布局

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView02"
    android:layout_marginTop="29dp"
    android:text="Choose Interested Majors"
    android:onClick="onCheckboxClicked" />

打开新活动的复选框我需要做些什么?

1 个答案:

答案 0 :(得分:0)

你的android:onClick函数有不同的名称。会发生什么是Android没有找到您从布局调用的方法,因为名称不同。

在布局和代码中均等地命名方法,它将起作用。