gpt4 book ai didi

android - 如何修复 Android 上的替换方法错误?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:49 25 4
gpt4 key购买 nike

如果完美的话,我正在创建带有操作栏选项卡的 Android 应用程序,但我对 fragment 的替换方法有疑问。如何修复此错误?

系统显示:

The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment)

开始 Activity .java

package com.devlebactionbartab;

import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.drm.DrmStore.Action;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class StartActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);


ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setTitle("Tabbed Layout");


ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Fragment1");
ActionBar.Tab Frag2Tab = actionbar.newTab().setText("Fragment2");
ActionBar.Tab Frag3Tab = actionbar.newTab().setText("Fragment3");
ActionBar.Tab Frag4Tab = actionbar.newTab().setText("Fragment4");

Fragment fragment1 = new Fragment_1();
Fragment fragment2 = new Fragment_2();
Fragment fragment3 = new Fragment_3();
Fragment fragment4 = new Fragment_4();

Frag1Tab.setTabListener(new MyTabsListener(fragment1));
Frag2Tab.setTabListener(new MyTabsListener(fragment2));
Frag3Tab.setTabListener(new MyTabsListener(fragment3));
Frag4Tab.setTabListener(new MyTabsListener(fragment4));

actionbar.addTab(Frag1Tab);
actionbar.addTab(Frag2Tab);
actionbar.addTab(Frag3Tab);
actionbar.addTab(Frag4Tab);
}


class MyTabsListener implements ActionBar.TabListener{

public Fragment fragment;

//constractor
public MyTabsListener (Fragment fragment){

this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
}

最佳答案

 android.app.FragmentTransaction.replace(int id,android.app.Fragment fragment);

您的方法正在寻找来自 android.app.Fragment 的 fragment ,您已经导入了 android.support.v4.app.Fragment。我的建议是您也从 v4 支持导入 FragmentTransaction。

步骤:

删除:import android.app.FragmentTransaction;

添加:import android.support.v4.app.FragmentTransaction;

转到 eclipse->project->clean -> clean all.

v4 support FragmentTransaction

关于android - 如何修复 Android 上的替换方法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19140409/

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