Jump to content

Android app crashes after adding ButtonHandler?

Go to solution Solved by Er0r,
package com.example.tejas.lttsub;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button submit_btn;
    private EditText name_txt;
    private EditText age_txt;
    private EditText height_txt;
    private EditText weight_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setup_activity();
    }
    private void setup_activity() {
        //set up on screeen controls
        submit_btn =  findViewById(R.id.but);


        //assign handlers
        submit_btn.setOnClickListener(new ButtonHandler()); //runs fine without this line

    }
    private class ButtonHandler implements View.OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            Toast.makeText(MainActivity.this, "Working", Toast.LENGTH_SHORT).show();
            submit_btn.setText("success");
        }
    }
}

I Re-Wrote your code and it seems to work fin Maybe your xml file is written a bit wrong.

Hey guys, so I'm trying to make a sign up page, however whenever I add the Button handler to my button it crashes before it even loads the activity. So it isn't happening on the button click, but when the activity starts to load. If I remove the line where I attach my button handler to my button ("submit_btn.setOnClickListener(new ButtonHandler());")  it launches and runs fine.

Here is the code:

public class SignUp extends AppCompatActivity {

    private Button submit_btn;
    private EditText name_txt;
    private EditText age_txt;
    private EditText height_txt;
    private EditText weight_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        setup_activity();
    }

    private void setup_activity() {
        //set up on screeen controls
        submit_btn = (Button) findViewById(R.id.submit_btn);
        name_txt = (EditText) findViewById(R.id.name_text);
        age_txt = (EditText) findViewById(R.id.age_txt);
        weight_txt = (EditText) findViewById(R.id.weight_text);
        height_txt = (EditText) findViewById(R.id.height_text);

        //assign handlers
        submit_btn.setOnClickListener(new ButtonHandler()); //runs fine without this line

    }

   private class ButtonHandler implements View.OnClickListener
   {
        @Override
        public void onClick(View view) 
        {
            submit_btn.setText("success");
        }
    }
}

Would appreciate any help, been struggling with this for way too long. Thanks in advance! :)

Link to post
Share on other sites

package com.example.tejas.lttsub;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button submit_btn;
    private EditText name_txt;
    private EditText age_txt;
    private EditText height_txt;
    private EditText weight_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setup_activity();
    }
    private void setup_activity() {
        //set up on screeen controls
        submit_btn =  findViewById(R.id.but);


        //assign handlers
        submit_btn.setOnClickListener(new ButtonHandler()); //runs fine without this line

    }
    private class ButtonHandler implements View.OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            Toast.makeText(MainActivity.this, "Working", Toast.LENGTH_SHORT).show();
            submit_btn.setText("success");
        }
    }
}

I Re-Wrote your code and it seems to work fin Maybe your xml file is written a bit wrong.

Link to post
Share on other sites

20 minutes ago, Er0r said:

Snip

Ah, yep, I feel like an idiot.  I didn't even think it could be an XML problem! I deleted my "submit_btn" and replaced it with "submit_button" so I was calling a button that no longer existed. Thank you!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×