0

嗨,我对 tab+fragment 有疑问,这里首先我有将创建选项卡的类:

public class TestSwipeABActivity extends FragmentActivity {

FragmentTransaction transaction;
static ViewPager mViewPager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Fragment tabOneFragment = new TabOne();
    Fragment tabTwoFragment = new TabTwo();

    PagerAdapter mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
    mPagerAdapter.addFragment(tabOneFragment);
    mPagerAdapter.addFragment(tabTwoFragment);

    //transaction = getSupportFragmentManager().beginTransaction();

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setCurrentItem(0);

    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    getActionBar().setSelectedNavigationItem(position);
                }
            });

    ActionBar ab = getActionBar();
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    Tab tab1 = ab.newTab().setText("Tab One").setTabListener(new TabListener<TabOne>(
                    this, "tabone", TabOne.class));

    Tab tab2 = ab.newTab().setText("Tab Two").setTabListener(new TabListener<TabTwo>(
                    this, "tabtwo", TabTwo.class));

    ab.addTab(tab1);
    ab.addTab(tab2);
}

public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    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) {
        // 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) {
        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.
    }

    public void onTabReselected(Tab arg0,
            android.app.FragmentTransaction arg1)
    {
        // TODO Auto-generated method stub

    }

    public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1)
    {
        // TODO Auto-generated method stub
        mViewPager.setCurrentItem(arg0.getPosition());
    }

    public void onTabUnselected(Tab arg0,
            android.app.FragmentTransaction arg1)
    {
        // TODO Auto-generated method stub

    }
}

public class PagerAdapter extends FragmentPagerAdapter {

    private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();

    public PagerAdapter(FragmentManager manager) {
        super(manager);
    }

    public void addFragment(Fragment fragment) {
        mFragments.add(fragment);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }
}

}

然后这里是每个选项卡的片段,例如两个:

public class TabTwo extends Fragment
{

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.tabtwo, container, false);


        Button  Activity1= (Button) view.findViewById(R.id.button1);

        Activity1.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {

                    Intent intent = new Intent().setClass(this,ABActivity.class);
                    startActivity(intent);
              }
        }); 

        return view;
    }
}

错误是:The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (new View.OnClickListener(){}, Class<ABActivity>)

我尝试将上下文更改为 TabTwo.this,tabtwo.getcontext.this,但没有,eclipse 说要更改 .setclassName 但不起作用。

如果你能帮忙...谢谢!!!

4

2 回答 2

0

嗨,我只有一个解决方案:

 public void onClick(View view) {
              Activity activity = getActivity();

                Intent intent = new Intent().setClass(activity, ABActivity.class);
                startActivity(intent);
          }

解释:“另一个区别是 Fragment 不是 Context 的子类。这意味着 Fragment 不能作为组件在您的应用程序中启动,因此必须始终存在于 Activity 中。这也意味着每当您需要Fragment 内部的上下文,您需要访问父 Activity。您可以使用 getActivity() 方法,就像我们在 Fragment 按钮的 OnClickListener 回调中所做的那样。您需要注意,因为 getActivity() 可以返回 null取决于 Fragment 在 Activity 生命周期中的位置。因此,您还应该在使用之前检查 Activity 是否为空。

来自:http : //neilgoodman.net/2012/01/29/working-with-fragments-on-android-part-1/

于 2013-01-24T11:54:39.330 回答
0

试试这个

Intent intent = new Intent(getActivity(),ABActivity.class);

startActivity(intent );

于 2013-01-24T11:58:00.517 回答