title: Cannot Create TabListener for a Tab in Android
body: <p>the problem that I have is that one of my tab listeners say</p>
  
  <pre><code>Bound mismatch: The type MainActivity is not a valid substitute 
  for the bounded parameter &lt;T extends Fragment&gt; of the type 
  MainActivity.TabListener&lt;T&gt;
  </code></pre>
  
  <p>Here is part of my MainActivity.java code:</p>
  
  <pre><code>package com.example.currencyconverter;
  
  import android.app.ActionBar;
  import android.app.Activity;
  import android.app.Fragment;
  import android.app.FragmentTransaction;
  import android.app.ActionBar.Tab;
  import android.app.ActionBar.TabListener;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  
  public class MainActivity extends Activity implements OnClickListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      addListenerOnButton();
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
  
      getActionBar().setDisplayShowHomeEnabled(false);
      getActionBar().setDisplayShowTitleEnabled(false);
      final ActionBar actionBar = getActionBar();
      Tab tab = actionBar.newTab()
              .setText("Currencies")
              .setTabListener(new TabListener&lt;Fragment1&gt;(
                      this, "Currencies", Fragment1.class));
      actionBar.addTab(tab);
  
      Tab tab1 = actionBar.newTab()
          .setText("Converter")
          .setTabListener(new TabListener&lt;Fragment3&gt;(
                  this, "Converter", Fragment3.class));
      actionBar.addTab(tab1);
  
      Tab tab2 = actionBar.newTab()
              .setText("News")
              .setTabListener(new TabListener&lt;Fragment2&gt;(
                      this, "News", Fragment2.class));
      actionBar.addTab(tab2);
  
  
      /*ActionBar actionBar = getActionBar();
      actionBar.setSubtitle("Created By Rohit Nandakumar");
      actionBar.setTitle("Currency Converter");
      // Specify that tabs should be displayed in the action bar.
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      ActionBar.Tab Frag1 = actionBar.newTab().setText("Converter");
      ActionBar.Tab Frag2 = actionBar.newTab().setText("Currencies");
      ActionBar.Tab Frag3 = actionBar.newTab().setText("News");
  
      Fragment fragment1 = new Fragment1();
      Fragment fragment2 = new Fragment2();
      Fragment fragment3 = new Fragment3();
  
      Frag1.setTabListener(new MyTabsListener(fragment1));
      Frag2.setTabListener(new MyTabsListener(fragment2));
      Frag3.setTabListener(new MyTabsListener(fragment3));
  
      actionBar.addTab(Frag1);
      actionBar.addTab(Frag2);
      actionBar.addTab(Frag3); */
  
  
      // Create a tab listener that is called when the user changes tabs.
      /*ActionBar.TabListener tabListener = new ActionBar.TabListener() {
          public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
              String n = tab.getText().toString();
              Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
              if (n.equals("Converter")) {
                  startActivity(new Intent (MainActivity.this, MainActivity.class));  
                  } 
              if (n.equals("Currencies")) {
                  startActivity(new Intent (MainActivity.this, FirstActivity.class)); 
                  }
              if (n.equals("News")) {
                  startActivity(new Intent (MainActivity.this, FirstActivity.class)); 
                  }
          }
  
          public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
              // hide the given tab
          }
  
          public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
              // probably ignore this event
          }
      };
  
      // Add 3 tabs, specifying the tab's text and TabListener
      for (int i = 0; i &lt; 3; i++) {
          actionBar.addTab(
                  actionBar.newTab()
                          .setText(tabTitle[i])
                          .setTabListener(tabListener));
      } */
  
  }
  public static class TabListener&lt;T extends Fragment&gt; implements ActionBar.TabListener {
      private final Activity mActivity;
      private final String mTag;
      private final Class&lt;T&gt; mClass;
  
      /** Constructor used each time a new tab is created.
        * @param activity  The host Activity, used to instantiate the fragment
        * @param tag  The identifier tag for the fragment
        * @param clz  The fragment's Class, used to instantiate the fragment
        */
      public TabListener(Activity activity, String tag, Class&lt;T&gt; clz) {
          mActivity = activity;
          mTag = tag;
          mClass = clz;
      }
  
      /* The following are each of the ActionBar.TabListener callbacks */
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
          Fragment mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
          // Check if the fragment is already initialized
          if (mFragment == null) {
              // If not, instantiate and add it to the activity
              mFragment = Fragment.instantiate(mActivity, mClass.getName());
              ft.add(android.R.id.content, mFragment, mTag);
          } else {
              // If it exists, simply attach it in order to show it
              ft.attach(mFragment);
          }
      }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          Fragment mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
          if (mFragment != null) {
              // Detach the fragment, because another one is being attached
              ft.detach(mFragment);
          }
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
          // User selected the already selected tab. Usually do nothing.
      }
  }
  class MyTabsListener implements ActionBar.TabListener {
  
      public Fragment fragment;
  
      public MyTabsListener(Fragment fragment) {
          this.fragment = fragment;
      }
  
  
      @Override
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
      }
  
      @Override
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
          ft.replace(R.id.fragment_container, fragment);
          }
  
      @Override
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          ft.remove(fragment);
  
  
      }
      }
  </code></pre>
  
  <p>The problem occurs right here:</p>
  
  <pre><code>tab = actionBar.newTab()
      .setText("Converter")
      .setTabListener(new TabListener&lt;MainActivity&gt;(
              this, "Converter", MainActivity.class));
  actionBar.addTab(tab);
  </code></pre>
  
  <p>This part:</p>
  
  <pre><code>    .setTabListener(new TabListener&lt;MainActivity&gt;(
              this, "Converter", MainActivity.class));
  </code></pre>
  
  <p>is where I am getting my error.</p>
  
  <p>I have searched all over Google for the past week, but I could not find the answer.  Any help is greatly appreciated.</p>