Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
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

<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

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. Any help is appreciated

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

In your case, you have declared an onClick handler for your button on XML with this line:

 android:onClick="onClick"

So, you now have to create a method name onClick() in your activity and do your code there like this:

public void onClick(View v) {
  Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
  startActivity(reg_intent);
}
  1. Remove this line from your 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. You have to use one at a time.

over 4 years ago · Santiago Trujillo Report

0

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

To use android:onClick correctly, you could refer to the official link

over 4 years ago · Santiago Trujillo Report

0

Try getting current context using getContext() and implement as.

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. Both can not co-exist.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!