title: How to find button by id from Fragment
body: <p>I'm making an android app and I have a button that won't react when I press it. The button is supposed to bring up an alertdialog box. I'm very confused as I have an OnClickListener and the dialog box is instantiated. </p>
  
  <p>Here is the code from the Java file:</p>
  
  <pre><code>public class tab1Expenses extends Fragment {
  
      Button btnEx;
  
      public class button extends AppCompatActivity {
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.tab1expense);
              btnEx = (Button)findViewById(R.id.btnEx);
  
              btnEx.setOnClickListener(new View.OnClickListener(){
  
                  @Override
                  public void onClick(View v) {
                      View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null);
  
                      AlertDialog.Builder add = new AlertDialog.Builder(button.this);
                      add.setView(view);
  
                      add.setCancelable(true)
                          .setTitle("Enter Expense:")
                          .setMessage("Expense Name:")
                          .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
  
                              }
                          });
  
                      Dialog dialog = add.create();
                      dialog.show();
                  }
              });
          }
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.tab1expense, container, false);
          return rootView;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>And here is my XML file that is related to the java file: </p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.ojemz.expensetracker.tab1Expenses$button"
  android:background="@android:color/darker_gray"&gt;
  
  &lt;Button
      android:text="Add Expense"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/btnEx"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:width="800dp"
      android:textAppearance="@style/TextAppearance.AppCompat.Widget.Switch"
      android:background="@android:color/black"
      android:textColor="@android:color/holo_green_light" /&gt;
  
  &lt;ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentBottom="true"
      android:layout_alignParentStart="true"
      android:layout_marginBottom="17dp"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical" &gt;
  
          &lt;!-- TextView and other stuff --&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p></p>