user2507301
user2507301

Reputation: 153

Cannot Create TabListener for a Tab in Android

the problem that I have is that one of my tab listeners say

Bound mismatch: The type MainActivity is not a valid substitute 
for the bounded parameter <T extends Fragment> of the type 
MainActivity.TabListener<T>

Here is part of my MainActivity.java 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<Fragment1>(
                    this, "Currencies", Fragment1.class));
    actionBar.addTab(tab);

    Tab tab1 = actionBar.newTab()
        .setText("Converter")
        .setTabListener(new TabListener<Fragment3>(
                this, "Converter", Fragment3.class));
    actionBar.addTab(tab1);

    Tab tab2 = actionBar.newTab()
            .setText("News")
            .setTabListener(new TabListener<Fragment2>(
                    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 < 3; i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(tabTitle[i])
                        .setTabListener(tabListener));
    } */

}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> 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<T> 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);


    }
    }

The problem occurs right here:

tab = actionBar.newTab()
    .setText("Converter")
    .setTabListener(new TabListener<MainActivity>(
            this, "Converter", MainActivity.class));
actionBar.addTab(tab);

This part:

    .setTabListener(new TabListener<MainActivity>(
            this, "Converter", MainActivity.class));

is where I am getting my error.

I have searched all over Google for the past week, but I could not find the answer. Any help is greatly appreciated.

Upvotes: 3

Views: 4967

Answers (3)

mrtn
mrtn

Reputation: 31

check your imports :

import android.app.Fragment;

vs

import android.support.v4.app.Fragment

Upvotes: 3

Xaver Kapeller
Xaver Kapeller

Reputation: 49817

You are getting this error because your TabListener class is expecting a type parameter which derives from Fragment indicated by

TabListener<T extends Fragment>

It works for Fragment1 and Fragment2 because they are both derivatives of the Fragment class. But since MainActivity derives from an Activity this will not work. I suggest you implement your TabListener without generics. From the code you posted, there doesn't seem to be a real need for this anyway.

Make these changes to your code and try if it works:

public static class TabListener implements ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class mClass;

        public TabListener(Activity activity, String tag, Class clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }
        .
        .
        .
}

Upvotes: 0

A--C
A--C

Reputation: 36449

Your TabListener class requires that its generic bound (the class name that you specify between the <>) be derived from the Fragment class.

Since Activity is not a Fragment in any way shape or form, you get that compile time error.

On the other hand,

Tab tab = actionBar.newTab()
          .setText("Currencies")
          .setTabListener(new TabListener<Fragment1>(
           this, "Currencies", Fragment1.class));

works because Fragment1 is almost certainly a Fragment subclass.

Also since you say that you spent a lot of time on this issue, I recommend learning Java (or any OOP language, but Android uses Java as you can see here) before doing stuff like this.

Upvotes: 0

Related Questions