title: "Button button1 = (Button)findViewById(R.id.button_1) " goes run in Android
body: <p>I'm a beginner in Android , so please pardon my ignorance.
  As I was learning Android programming,I followed the book and the codes are as follows:</p>
  <h4>FirstActivity.java</h4>
  <pre class="lang-java prettyprint-override"><code>package com.example.activitytest;
  
  import androidx.appcompat.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Toast;
  public class FirstActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.first_layout);
          Button button1 = (Button)findViewById(R.id.button_1);
          button1.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v){
                  Toast.makeText(FirstActivity.this, &quot;You clicked Button 1&quot;, Toast.LENGTH_SHORT).show();
              }
          });
      }
  }
  
  </code></pre>
  <h4>AndroidManifest.xml</h4>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.example.activitytest&quot;&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.Activitytest&quot;&gt;
          &lt;activity
              android:name=&quot;.FirstActivity&quot;
              android:label=&quot;This is FirstActivity&quot;
              android:exported=&quot;true&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name = &quot;android.intent.action.MAIN&quot;/&gt;
                  &lt;category android:name = &quot;android.intent.category.LAUNCHER&quot;/&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <h4>first_layout.xml</h4>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:orientation=&quot;vertical&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/button_1&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;@string/button_1&quot;
          tools:ignore=&quot;MissingConstraints&quot;
          tools:layout_editor_absoluteX=&quot;0dp&quot;
          tools:layout_editor_absoluteY=&quot;1dp&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>And android studio reports that:</p>
  <pre><code>error: cannot find symbol
          Button button1 = (Button)findViewById(R.id.button_1);
          ^
    symbol:   class Button
    location: class FirstActivity
  </code></pre>
  <p>and then I search stackoverflow to find the method to debug.
  I once tried this method:</p>
  <pre class="lang-java prettyprint-override"><code>View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  Button buttonClick = (Button)rootView.findViewById(R.id.button);
  buttonClick.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          onButtonClick((Button) view);
      }
  });
  </code></pre>
  <p>But it still does not work.As I am just a beginner, I can't find a way to debug.
  Can someone help me? Thank you for that.</p>