【发布时间】:2017-09-07 22:52:45
【问题描述】:
我正在尝试将 onClick 方法 front() 添加到我的 Button。但是,当我单击 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" />
Register.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();
}
}
知道问题出在哪里吗?
【问题讨论】:
-
我建议你直接在代码中设置
onClickListener是最好的方法,它永远不会失败,你也不需要在主代码中声明这么多函数。