Aaron T
Aaron T

Reputation: 1102

Android app closes due because of button.setOnClickListener(this)

I'm going through a tutorial that's teaching me about Android App development. I've been going through it all just fine but for some reason when I run this activity the app closes and says that it has stopped working. As far as I can tell I have copied the person's code correctly, but his works. I found that the problem is with the line inside onCreate that says tryCmd.setOnClickListener(this);

If I comment out that single line, the activity works fine(but just doesn't do anything when I click the button, obviously). The app works fine with the togglebutton's onClickListener statement. Can anyone tell me what is wrong? Thanks.

Here is all the code from my activity.java:

package com.example.testapp;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Text extends Activity implements View.OnClickListener{

    EditText input;
    Button tryCmd;
    ToggleButton passTog;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);
        //Call the method to assign variables to the elements.
        assignVariables(); 
        //Initialize the button and ToggleButton to work with the onClick method.
        passTog.setOnClickListener(this);
        tryCmd.setOnClickListener(this);        

    }

    private void assignVariables() {
        // TODO Auto-generated method stub
        input = (EditText) findViewById(R.id.etCommands);
        Button tryCmd = (Button) findViewById(R.id.bResults);
        passTog = (ToggleButton) findViewById(R.id.tbPassword);
        display = (TextView) findViewById(R.id.tvResults);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.bResults:
            String check = input.getText().toString();
            if (check.contentEquals("left")){
                display.setGravity(Gravity.LEFT);
            }else if(check.contentEquals("center")){
                display.setGravity(Gravity.CENTER);
            }else if(check.contentEquals("right")){
                display.setGravity(Gravity.RIGHT);
            }else if(check.contentEquals("blue")){
                display.setTextColor(Color.BLUE);
            }else if(check.contains("WTF")){
                Random crazy = new Random();
                display.setText("WTF!?!?");
                display.setTextSize(crazy.nextInt(75));
                display.setTextColor(Color.rgb(crazy.nextInt(255), crazy.nextInt(255), crazy.nextInt(255)));
                switch(crazy.nextInt(3)){
                case 0:
                    display.setGravity(Gravity.LEFT);
                    break;
                case 1:
                    display.setGravity(Gravity.CENTER);                     
                    break;
                case 2:
                    display.setGravity(Gravity.RIGHT);                      
                    break;
                }
            }else{
                display.setText("invalid");
                display.setTextColor(Color.BLACK);
                display.setGravity(Gravity.CENTER);
            }

            //Clear the input box if it doesn't contain WTF.
            if(!check.contains("WTF")){
                input.setText("");
            }
            break;

        case R.id.tbPassword:
            if (passTog.isChecked()){
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            } else{
                input.setInputType(InputType.TYPE_CLASS_TEXT);
            }
            break;
        }
    }

}

Upvotes: 0

Views: 55

Answers (3)

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

There is Problem with this line..

Button tryCmd = (Button) findViewById(R.id.bResults);

You Are creating local Variable and Assigning value to new local Variable... So the global variable is still NULL..

So Remove that line from assignVariables() function..

Try This code...

private void assignVariables() {
    // TODO Auto-generated method stub
    input = (EditText) findViewById(R.id.etCommands);
    tryCmd = (Button) findViewById(R.id.bResults);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    display = (TextView) findViewById(R.id.tvResults);
}  

Upvotes: 0

MHP
MHP

Reputation: 2731

you define tryCmd as local variable so it detect only in assignVariables() method.you should change it to global variable.(as you define but not use)

private void assignVariables() {
    // TODO Auto-generated method stub
    input = (EditText) findViewById(R.id.etCommands);
    Button tryCmd = (Button) findViewById(R.id.bResults);//error in this line you define it as local variable delete Button before it
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    display = (TextView) findViewById(R.id.tvResults);
}  

edited:

private void assignVariables() {
    // TODO Auto-generated method stub
    input = (EditText) findViewById(R.id.etCommands);
    tryCmd = (Button) findViewById(R.id.bResults);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    display = (TextView) findViewById(R.id.tvResults);
}  

Upvotes: 1

Mr X
Mr X

Reputation: 1063

you are initialize tryCmd as local variable. and set onClickListner onglobal variable tryCmd

remove Button from this line

Button tryCmd = (Button) findViewById(R.id.bResults);

and change it

tryCmd = (Button) findViewById(R.id.bResults);

Upvotes: 1

Related Questions