I’ve been working on developing an app for a while, and everything was going well. However, when I added an onCLickListener for a button in one of my .xml files(not main.xml), it started crashing as soon as I tried to open it in the emulator. All the code for the onClickListener is the same as for the other two buttons I have. here’s the code for my problematic listener:
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(bt3listener);
private OnClickListener bt3listener = new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.main);
}
};
The only way that any of this code differs from my other onClickListeners is that it references a button in a different file than main.xml. It crashes before displays any of the ui except a black screen with my project name on the top, and gives me a prompt that says “un
It have to be int the layout you set with
setContentView(), in order to retrieve it withfindViewById()usingthisor use aninflaterin order to get the layout in which theButtonis declared, and use the returnedviewwithfindViewById().