gpt4 book ai didi

android - 无法在 Android 中为选项卡创建 TabListener

转载 作者:太空狗 更新时间:2023-10-29 12:46:55 27 4
gpt4 key购买 nike

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

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

是我出错的地方。

过去一周我在 Google 上搜索了所有内容,但找不到答案。非常感谢任何帮助。

最佳答案

检查你的导入:

import android.app.Fragment;

对比

import android.support.v4.app.Fragment

关于android - 无法在 Android 中为选项卡创建 TabListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17373251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com