gpt4 book ai didi

java.lang.IllegalStateException : Could not find method in a parent or ancestor Context for android:onClick attribute 错误

转载 作者:行者123 更新时间:2023-11-29 19:16:30 25 4
gpt4 key购买 nike

我正在尝试将 onClick 方法 front() 添加到我的 Button。但是,当我单击按钮时,它会返回此错误:

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

这是我的 xml:

<Button
android:id="@+id/front"
android:onClick="front"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text" />

注册.java:

public class Register extends AppCompatActivity {

private Button front;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

front = (Button) findViewById(R.id.front);
}

private void front(View v) {
Toast.makeText(Register.this, "String", Toast.LENGTH_LONG).show();
}

}

知道问题出在哪里吗?

最佳答案

一定要正确书写属性值onClick的语法。就我而言,我忘记了字符 }

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:onClick="@{() -> viewModel.onItemClick(position)}">

GL

关于java.lang.IllegalStateException : Could not find method in a parent or ancestor Context for android:onClick attribute 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364776/

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