Could not find method XXXX(View ) in a parent or ancester Context for android:onClick attribute

1. The button code details in xml are as follows:

<Button
        android:text="是否开启"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:onClick="isBlueToothEnable (MainActivity)" />

2. The click method of "isBlueToothEnable" in Activity is as follows:

public void isBlueToothEnable(View view){
        boolean ret = mcontroller.getBlueToothStatus();
        showToast("是否打开了蓝牙?"+ret);
    }

3. The solution is to modify the content in xml:
before modification:

android:onClick="isBlueToothEnable (MainActivity)" />

After modification:

android:onClick="isBlueToothEnable" />

4. Recompile and run, the problem is solved.

Guess you like

Origin blog.csdn.net/weiybin/article/details/130322873