0

アプリを作成していて、問題に遭遇しました。私のアプリのフラグメントは、選択するとテキストを表示するはずですが、そうではありません。これが私のコードです:

このクラスは、選択時にテキストを表示するフラグメントです。

TestFragment.class:

public class TestFragment extends SherlockFragment {

public static final String EXTRA_TITLE = "title";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    TextView txt = new TextView(inflater.getContext());
    txt.setGravity(Gravity.CENTER);
    txt.setText("Fragment");

    if (getArguments() != null && getArguments().containsKey(EXTRA_TITLE)) {
        txt.setText(getArguments().getString(EXTRA_TITLE));
    }
    return txt;
}

public static Bundle createBundle(String title) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_TITLE, title);
    return bundle;
}

}

これは、タブが追加されるメインクラスです。以前に作成した TabSwipeActivity を拡張します。メインクラス:

public class TaskMain extends TabSwipeActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addTab("Urgent", TestFragment.class,
            TestFragment.createBundle("TestFragment"));
    addTab("Due", TestFragment.class,
            TestFragment.createBundle("Fragment 2"));
    addTab("Unimportant", TestFragment.class,
            TestFragment.createBundle("Fragment 3"));
    addTab("Shopping", TestFragment.class,
            TestFragment.createBundle("Fragment 4"));
    addTab("Notes", TestFragment.class,
            TestFragment.createBundle("Fragment 5"));


}


@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflate = getSupportMenuInflater();
    inflate.inflate(R.menu.activity_task_main, menu);

    return true;
}

}

そして、これは私の主なアクティビティが拡張する TabSwipeActivity です。TabSwipeActivity.class:

public abstract class TabSwipeActivity extends SherlockFragmentActivity {

private ViewPager mViewPager;
private TabsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    /*
     * Create the ViewPager and our custom adapter
     */
    mViewPager = new ViewPager(this);
    adapter = new TabsAdapter(this, mViewPager);
    mViewPager.setAdapter(adapter);
    mViewPager.setOnPageChangeListener(adapter);

    /*
     * We need to provide an ID for the ViewPager, otherwise we will get an
     * exception like:
     * 
     * java.lang.IllegalArgumentException: No view found for id 0xffffffff
     * for fragment TestFragment{40de5b90 #0 id=0xffffffff
     * android:switcher:-1:0} at
     * android.support.v4.app.FragmentManagerImpl.moveToState
     * (FragmentManager.java:864)
     * 
     * The ID 0x7F04FFF0 is large enough to probably never be used for
     * anything else
     */
    mViewPager.setId(0x7F04FFF0);

    super.onCreate(savedInstanceState);

    /*
     * Set the ViewPager as the content view
     */


    setContentView(mViewPager);
}

/**
 * Add a tab with a backing Fragment to the action bar
 * 
 * @param titleRes
 *            A string resource pointing to the title for the tab
 * @param fragmentClass
 *            The class of the Fragment to instantiate for this tab
 * @param args
 *            An optional Bundle to pass along to the Fragment (may be null)
 */
protected void addTab(int titleRes,
        Class<? extends SherlockFragment> fragmentClass, Bundle args) {
    adapter.addTab(getString(titleRes), fragmentClass, args);
}

/**
 * Add a tab with a backing Fragment to the action bar
 * 
 * @param titleRes
 *            A string to be used as the title for the tab
 * @param fragmentClass
 *            The class of the Fragment to instantiate for this tab
 * @param args
 *            An optional Bundle to pass along to the Fragment (may be null)
 */
protected void addTab(CharSequence title,
        Class<? extends SherlockFragment> fragmentClass, Bundle args) {
    adapter.addTab(title, fragmentClass, args);
}

private void setContent() {

setContentView(R.layout.activity_task_new);
}


private static class TabsAdapter extends FragmentPagerAdapter implements
        TabListener, ViewPager.OnPageChangeListener {

    private final SherlockFragmentActivity mActivity;
    private final ActionBar mActionBar;
    private final ViewPager mPager;

    /**
     * @param fm
     * @param fragments
     */
    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        this.mActivity = activity;
        this.mActionBar = activity.getSupportActionBar();
        this.mPager = pager;

        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }

    private static class TabInfo {
        public final Class<? extends SherlockFragment> fragmentClass;
        public final Bundle args;

        public TabInfo(Class<? extends SherlockFragment> fragmentClass,
                Bundle args) {
            this.fragmentClass = fragmentClass;
            this.args = args;
        }
    }

    private List<TabInfo> mTabs = new ArrayList<TabInfo>();

    public void addTab(CharSequence title,
            Class<? extends SherlockFragment> fragmentClass, Bundle args) {
        final TabInfo tabInfo = new TabInfo(fragmentClass, args);

        Tab tab = mActionBar.newTab();
        tab.setText(title);
        tab.setTabListener(this);
        tab.setTag(tabInfo);

        mTabs.add(tabInfo);

        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public SherlockFragment getItem(int position) {
        final TabInfo tabInfo = mTabs.get(position);
        return (SherlockFragment) SherlockFragment.instantiate(mActivity,
                tabInfo.fragmentClass.getName(), tabInfo.args);
    }

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

    public void onPageScrollStateChanged(int arg0) {
    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    public void onPageSelected(int position) {
        /*
         * Select tab when user swiped
         */
        mActionBar.setSelectedNavigationItem(position);


    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        /*
         * Slide to selected fragment when user selected tab
         */
        TabInfo tabInfo = (TabInfo) tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tabInfo) {
                mPager.setCurrentItem(i);
            }
        }

    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

}

4

1 に答える 1

2

SherlockFragment を拡張します

org.holoeverywhere.app.Fragment から拡張する必要があります

SherlockFragmentActivity を拡張します

org.holoeverywhere.app.Activity

于 2013-03-13T08:44:59.103 に答える