3

我遇到的问题是我的一位标签听众说

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

这是我的 MainActivity.java 代码的一部分:

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);


    }
    }

问题就出现在这里:

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

这部分:

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

是我得到错误的地方。

过去一周我在谷歌上搜索过,但我找不到答案。任何帮助是极大的赞赏。

4

3 回答 3

3

检查你的进口:

import android.app.Fragment;

对比

import android.support.v4.app.Fragment
于 2014-04-04T07:24:41.127 回答
0

您收到此错误是因为您的 TabListener 类需要一个派生自 Fragment 的类型参数,由

TabListener<T extends Fragment>

它适用于 Fragment1 和 Fragment2,因为它们都是 Fragment 类的衍生物。但是由于 MainActivity 派生自 Activity 这将不起作用。我建议你在没有泛型的情况下实现你的 TabListener。从您发布的代码来看,无论如何似乎都没有真正的需要。

对您的代码进行这些更改并尝试它是否有效:

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;
        }
        .
        .
        .
}
于 2013-06-28T20:58:55.640 回答
0

您的TabListener类要求其通用绑定(您在 之间指定的类名 <>)派生自Fragment 该类。

由于Activity不是任何形状Fragment或形式,因此您会收到编译时错误。

另一方面,

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

有效,因为Fragment1几乎可以肯定它是一个Fragment子类。

另外,既然你说你在这个问题上花了很多时间,我建议在做这样的事情之前学习 Java(或任何 OOP 语言,但 Android 使用 Java,如你所见)。

于 2013-06-28T20:47:42.927 回答