James Zhang
James Zhang

Reputation: 29

app keeps stopping, but cannot find any problems

I want to debug my project, but it keeps stopping, I tried debugging it but cannot find any problems code:

TextView colorTextView;
Button plumButton;
Button blueButton;
Button goldButton;
    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    plumButton = findViewById(R.id.plumButton);         
    plumButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v == plumButton){
                 colorTextView.setText( "Plum is a deep purple color. It is used in weedings. " );
            }
        }
    });
}

What's wrong in my code or I is it a bug in AVD?

Upvotes: 0

Views: 49

Answers (2)

Anwar Elsayed
Anwar Elsayed

Reputation: 493

you must initialize textView you are trying to use

add this line of code :

colorTextView = findViewById(R.id.colorTextView);

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

In onClickListener of plumButton, your are trying to set text to colorTextView, but you have not initialised at-least not in above snippet.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    plumButton = findViewById(R.id.plumButton);
    colorTextView = findViewById(R.id.colorTextView); // add this line         
    plumButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v == plumButton){
                 colorTextView.setText( "Plum is a deep purple color. It is used in weedings. " );
            }
        }
    });
}

Upvotes: 2

Related Questions