title: How to set text of android button in onCreate Method?
body: <p>I want to set the text of my button in onCreate Method, but it doesn't work.</p>
  <p>Code in MainActivity-Class:</p>
  <pre><code>Button button;
  </code></pre>
  <p>Code in onCreate:</p>
  <pre><code>button = findViewById(R.id.button);
      button.setText(&quot;test&quot;);
  </code></pre>
  <p>Butto in xml:</p>
  <pre><code>&lt;Button
          android:id=&quot;@+id/button&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginTop=&quot;20dp&quot;
          android:alpha=&quot;0.5&quot;
          android:background=&quot;@color/design_default_color_secondary_variant&quot;
          android:onClick=&quot;checkPermissions&quot;
          android:textSize=&quot;30sp&quot; /&gt;
  </code></pre>
  <p>Here's the Exception:</p>
  <blockquote>
  <p>Caused by: java.lang.NullPointerException: Attempt to invoke virtual
  method 'void android.widget.Button.setText(java.lang.CharSequence)' on
  a null object reference</p>
  </blockquote>
  <p>I know that I can set the text of button in XML, but I want to set the text at app start via sharedpreferences, but it won't work if I can't even set a simple string onto it by calling this easy way.</p>