gpt4 book ai didi

java - 如何解决错误 : Could not find method onClick(View) in a parent or ancestor Context for android:onClick

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:58:06 25 4
gpt4 key购买 nike

我看到有一些类似的问题,但到目前为止,这些问题的答案对我没有帮助。完整错误:

java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_random'

类(StartActivity.java):

public class StartActivity extends AppCompatActivity {

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

public void onClick(View v) {
Log.d("DEBUG", "CLICKED " + v.getId());
}

}

XML(activity_start.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random Game"
android:id="@+id/button_random"
android:layout_gravity="center_horizontal"
android:onClick="onClick" />
</LinearLayout>

我已将 Activity 添加到 AndroidManifest.xml。我有以相同方式工作的类似 Activity ,我对这些没有任何问题...

有人看到我遗漏了什么或犯了错误吗?

最佳答案

就我而言,我错过了最后的右括号。

android:onClick="@{(v) -> CommentHandler.selectGallery(v)".

It should be like this.
android:onClick="@{(v) -> CommentHandler.selectGallery(v)}".

关于java - 如何解决错误 : Could not find method onClick(View) in a parent or ancestor Context for android:onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37136574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com