title: Android Programming - Fragments and Listeners
body: <p>I am following a tutorial on the internet and I have to make a listener for my buttons. The tutorial I am watching is very different from my code even though the video was posted not even a year ago. The video I am watching has no "fragment" code in it. In my code there is a fragment in my MainActivity.java. What is a fragment? In my mainactivity.java towards the end I get this:</p>
  
  <pre><code>public static class PlaceholderFragment extends Fragment {
  
      public PlaceholderFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  
          button1 = (Button)rootView.findViewById(R.id.button1);
          button2 = (Button)rootView.findViewById(R.id.button2);
          button3 = (Button)rootView.findViewById(R.id.button3);
  
          button1.setOnClickListener(listener);
          button2.setOnClickListener(listener);
          button3.setOnClickListener(listener);
  
          return rootView;
      }
  }
  </code></pre>
  
  <p>could anyone explain to me what a place holder fragment is? heres my code. I am getting an error here:</p>
  
  <pre><code>ERROR: "Toast.makeText(MainActivity.this, "toast", Toast.LENGTH_SHORT);"
  Message: "No enclosing instance of the type MainActivity is accessible in scope"
  </code></pre>
  
  <p>.
      private static OnClickListener listener = new OnClickListener()
      {
          @Override
          public void onClick(View v) {
              int id = v.getId();</p>
  
  <pre><code>        switch(id)
          {
          case R.id.button1:
              Toast.makeText(MainActivity.this, "toast", Toast.LENGTH_SHORT);
              break;
          case R.id.button2:
              break;
          case R.id.button3:
              break;
          default:
              break;
          }
      }
  };
  </code></pre>
  
  <p>HERES MY FULL CODE ( MainActivity.java ):</p>
  
  <pre><code>package com.example.androiddevelopmentonline_part2_1;
  
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.app.ActionBar;
  import android.support.v4.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.Toast;
  import android.os.Build;
  
  public class MainActivity extends ActionBarActivity {
  
  private static Button button1, button2, button3;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment())
                  .commit();
      }
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  
  private static OnClickListener listener = new OnClickListener()
  {
      @Override
      public void onClick(View v) {
          int id = v.getId();
  
          switch(id)
          {
          case R.id.button1:
              Toast.makeText(MainActivity.this, "toast", Toast.LENGTH_SHORT);
              break;
          case R.id.button2:
              break;
          case R.id.button3:
              break;
          default:
              break;
          }
      }
  };
  
  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {
  
      public PlaceholderFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  
          button1 = (Button)rootView.findViewById(R.id.button1);
          button2 = (Button)rootView.findViewById(R.id.button2);
          button3 = (Button)rootView.findViewById(R.id.button3);
  
          button1.setOnClickListener(listener);
          button2.setOnClickListener(listener);
          button3.setOnClickListener(listener);
  
          return rootView;
      }
  }
  }
  </code></pre>