我遇到的问题是我的一位标签听众说
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));
是我得到错误的地方。
过去一周我在谷歌上搜索过,但我找不到答案。任何帮助是极大的赞赏。