As pointed out by others, you did not set the layout through setContentView()before the call findViewById().
Why do I need it?
Because it Activity.findViewById()searches in the hierarchy for the representation of current activity. If you do not define a hierarchy of views, there is nothing to find. And when this method finds nothing, it returns null.
Therefore, you must add a layout after calling super.onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
button = (Button) findViewById(R.id.but);
user658042
source
share