我用按钮创建了一个新的活动,单击该按钮将产生以下错误:

java.lang.IllegalStateException: Could not find method sendPasswordEmail(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'sendPasswordEmailButton'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


这是活动XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/loginBackground"
    android:orientation="vertical"
    tools:context=".activities.ForgotPasswordActivity">


    <Button
        android:layout_marginTop="@dimen/vetical_margin_medium"
        android:layout_width="@dimen/centerButtonWidth"
        android:layout_height="@dimen/centerButtonHeight"
        android:background="@android:drawable/dialog_holo_dark_frame"
        android:textColor="@color/inputColour"
        android:text="Send"
        android:id="@+id/sendPasswordEmailButton"
        android:layout_gravity="center_horizontal"
        android:onClick="sendPasswordEmail"/>


</LinearLayout>


这是Activity的Java代码:

public class ForgotPasswordActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
    }

    public void sendPasswordEmail() {

         // do something
        }
    }
}


我以相同的方式实现的前两个活动都具有按钮和可以正常工作的关联的单击侦听器。

Compile Sdk Version: API 23: Android 6.0 (Marshmallow)
Min Sdk Version: 16
Target Sdk Version: 22


这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="crm.geoalertapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activities.ForgotPasswordActivity"
            android:label="@string/title_activity_forgot_password"
            android:parentActivityName=".activities.LoginActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activities.LoginActivity" />
        </activity>
    </application>

</manifest>


有人可以建议为什么onClick事件侦听器当前不与此活动一起使用吗?

最佳答案

您需要更改:

public void sendPasswordEmail() { ... }




public void sendPasswordEmail(View view) {
    // Handle click on the button
}

关于java - onClick事件监听器不适用于 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34379715/

10-13 06:18