Reputation: 29
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
Reputation: 493
you must initialize textView you are trying to use
add this line of code :
colorTextView = findViewById(R.id.colorTextView);
Upvotes: 0
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