我正在尝试将 onClick 方法添加front()到我的按钮。但是,当我单击按钮时,它会返回此错误:
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'
Run Code Online (Sandbox Code Playgroud)
这是我的 xml:
<Button
android:id="@+id/front"
android:onClick="front"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text" />
Run Code Online (Sandbox Code Playgroud)
注册.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();
}
}
Run Code Online (Sandbox Code Playgroud)
知道问题是什么吗?
你的front method活动应该是公开的。您现在已将其设为私有。
Android 开发者网站也对此进行了描述。
为了使其工作,该方法必须是公共的并接受 View 作为其唯一参数
public void front(View v) {
Toast.makeText(Register.this, "String", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
一定要正确写出属性值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)}">
Run Code Online (Sandbox Code Playgroud)
GL
| 归档时间: |
|
| 查看次数: |
3747 次 |
| 最近记录: |