If you want a reference to your button globally
then you should declare it outside then initialize it in onCreate
Button b1; //declare the button variable
public class SharedPreferencesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
b1 = (Button) findViewById(R.id.buttonSave); //initialize on creation
}
You should read more into the basics of Java
when learning Android
1
solved App crahing when Button declared outside onCreate method [duplicate]