简体   繁体   English

单击按钮时在 startActivity 上崩溃

[英]Crashing on startActivity when clicking button

Having the same issue.有同样的问题。 The button is initialized at the right time but for some reason, it crashes on click.该按钮在正确的时间初始化,但由于某种原因,它在单击时崩溃。 The xml file says the onClick handler is missing the related activity. xml 文件说 onClick 处理程序缺少相关活动。

xml xml

<Button
    android:id="@+id/start_reg_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="216dp"
    android:onClick="onClick"
    android:text="@string/need_a_new_account" />

Java Java

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

    mRegBtn = (Button) findViewById(R.id.start_reg_btn);

    mRegBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent reg_intent = new Intent(StartActivity.this, RegisterActivity.class);
            startActivity(reg_intent);
        }
    });
}

Very new to android programming. android 编程非常新。 Any help is appreciated任何帮助表示赞赏

There are two ways of solving your issue:有两种方法可以解决您的问题:

  1. When you declare onClick handler in your XML, you need to implement the method in your activity.当您在 XML 中声明 onClick 处理程序时,您需要在活动中实现该方法。

In your case, you have declared an onClick handler for your button on XML with this line:在您的情况下,您已使用此行为 XML 上的按钮声明了 onClick 处理程序:

 android:onClick="onClick"

So, you now have to create a method name onClick() in your activity and do your code there like this:因此,您现在必须在您的活动中创建一个方法名称 onClick() 并在那里执行您的代码,如下所示:

public void onClick(View v) {
  Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
  startActivity(reg_intent);
}
  1. Remove this line from your XML:从 XML 中删除此行:
 android:onClick="onClick"

and do what you did already:并做你已经做过的事情:

mRegBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
                startActivity(reg_intent);
            }
        });

So, the main concept is this you can't use android:onClick and setOnClickListener together.所以,主要概念是你不能同时使用android:onClicksetOnClickListener You have to use one at a time.您必须一次使用一个。

android:onClick and `setOnClickListener` can not used simultaneous.

To use android:onClick correctly, you could refer to the official link正确使用 android:onClick 可以参考 官方链接

Try getting current context using getContext() and implement as.尝试使用getContext()获取当前上下文并实现为。

public void onClick(View v) {
    Intent reg_intent = new Intent(v.getContext(), RegisterActivity.class);
    v.getContext().startActivity(reg_intent);
}

And either define onClick from xml file or define it programmatically using View.setOnClickListener .并从 xml 文件中定义onClick或使用View.setOnClickListener以编程方式定义它。 Both can not co-exist.两者不能共存。

Just Remove onClick from your Xml and try to run once again只需从 Xml 中删除 onClick 并尝试再次运行

Found the error that the TextEditinput.= TextInputLayout and that's why it was crashing.发现 TextEditinput.= TextInputLayout 的错误,这就是它崩溃的原因。 It was also working either by the onClick Method or SetuponClickListener so thank you for everyone and their help它也可以通过 onClick 方法或 SetuponClickListener 工作,所以谢谢大家和他们的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM