FragmentPagerAdapter 的麻烦和困境:构造函数未定义 [英] FragmentPagerAdapter troubles and woes: Constructor Undefined

查看:29
本文介绍了FragmentPagerAdapter 的麻烦和困境:构造函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新编写了一些代码以尝试克服错误(但现在应用程序在启动时崩溃).首先,我的类现在扩展了 FragmentActivity,以便我可以访问方法 getSupportFragmentManager();.然后,在创建时,我更改了我的构造函数:

I have reworked some of my code to try and get past the error (but now the app crashes on launch). First, my class now extends FragmentActivity so that I can access the method, getSupportFragmentManager();. Then, within on create, I changed my constructor:

mTabsAdapter = new TabsAdapter(this.getSupportFragmentManager(), this, mViewPager);

从那里,在我的子类中,我更改了代码以反映这一点:

From there, in my subclass, I changed the code to reflect this:

public TabsAdapter(FragmentManager fm, Activity activity, ViewPager pager) {
    super(fm);
    mContext = activity;
    mActionBar = activity.getActionBar();
    mViewPager = pager;
    mViewPager.setAdapter(this);
    mViewPager.setOnPageChangeListener(this);
}

正如我所说,它仍然崩溃,此时有什么想法吗?

开始原始问题:我有以下错误:构造函数 FragmentPagerAdapter(FragmentManager) 未定义"*

Begin original question: I have the following error: "The constructor FragmentPagerAdapter(FragmentManager) is undefined"*

当我尝试执行以下行时:

When I try to execute the following line:

super(activity.getFragmentManager()); 这一行跟随我的 public TabsAdapter(Activity activity, ViewPager pager) { 扩展 FragmentPagerAdapter.

super(activity.getFragmentManager()); this line follows my public TabsAdapter(Activity activity, ViewPager pager) { which is extending FragmentPagerAdapter.

基本上,我正在尝试获取我的 8+ 片段应用程序(8 个片段,一个活动,其中包含一组链接到每个片段的 ActionBar 选项卡 [为简洁起见,这里只看到一个loginTab"]).

Basically, I am trying to get my 8+ fragment app (8 fragment, one activity which hosts a set of ActionBar tabs linked to each fragment [only one is seen here for brevity's sake "loginTab"]).

这是我的全部代码:

我用作参考的页面:http://developer.android.com/reference/android/support/v4/view/ViewPager.html

http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

package com.davekelley.polling;

import java.util.ArrayList;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

public class Polling extends Activity {

    private ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
        final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);
        mTabsAdapter = new TabsAdapter(this, mViewPager);
        mTabsAdapter.addTab(bar.newTab().setText("Simple"),
        LoginFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Simple"),
        EconFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Simple"),
        PoliticsFragment.class, null);

        if (savedInstanceState != null) {
            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
        }
    }
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    }

    public static class TabsAdapter extends FragmentPagerAdapter
    implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList mTabs = new ArrayList();

        static final class TabInfo {
            private final Class clss;
            private final Bundle args;

            TabInfo(Class _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(Activity activity, ViewPager pager) {
            super(activity.getFragmentManager());
            mContext = activity;
            mActionBar = activity.getActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

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

        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }

        public void onPageScrollStateChanged(int state) {
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

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

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

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

        public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        }

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

知道这里发生了什么以及我能做些什么来解决它吗?我的 Fragment 类目前非常简单,但最终会从 SQL 服务器加载数据(同时,它们只是返回一个简单的膨胀布局).

Any idea what's going on here and what I can do to fix it? My Fragment classes are very simple at the moment, but will eventually load data from an SQL server (at the meantime, they just return a simple inflated layout).

推荐答案

我已经解决了这个问题.需要无数的解决方案.

I have solved the problem. It was a myriad of solutions that was required.

首先,我必须扩展 FragmentActivity.

First, I had to extend FragmentActivity.

public class Polling extends FragmentActivity {

然后,在创建我的 TabsAdapter 时,我必须向它(FragmentActivity 本身)发送一个调用:

Then, when creating my TabsAdapter, I had to send a call to this (the FragmentActivity itself):

mTabsAdapter = new TabsAdapter(this, mViewPager);

然后,在 TabsAdapter 子类中制作构造函数时,我不得不将参数更改为 FragmentActivity 而不是 Activity:

Then, when making the constructor in the TabsAdapter subclass, I had to change the parameter to FragmentActivity rather than Activity:

public TabsAdapter(FragmentActivity activity, ViewPager pager) {
    super(activity.getSupportFragmentManager());

最后,我不得不更改 Fragment 类中的 import 语句,以反映兼容性库的使用,而不是常规的片段包:

Finally, I had to change my import statement in my Fragment classes to reflect the use of the compatibility library rather than the regular fragment package:

import android.support.v4.app.Fragment;

所以,现在我的 ViewPager 函数.当然,单击每个单独的选项卡图标以在选项卡之间切换不再起作用(因此,我在发送此消息后立即着手解决该解决方案).无论如何,既然 ViewPager 起作用了,我可以继续处理我的应用程序中更紧迫的问题.感谢 LouisLouis 让我走上了正确的道路.我最终使用 CountingFragment.java 来帮助我看看我的错误在哪里,以及来自 ActionBarSherlock 代码的 FragmentTabsPager.java 以确定是否需要指定 FragmentActivity.

So, now my ViewPager functions. Of course, clicking on each individual tab icon to switch between tabs no longer works (so I am working on that solution as soon as I send this message). Regardless, now that the ViewPager functions, I can move on to more pressing issues in my app. Thanks to LouisLouis for starting me down the correct path. I eventually used CountingFragment.java to help me see where my errors were, as well as FragmentTabsPager.java from ActionBarSherlock's code to determine the need to specify FragmentActivity.

这篇关于FragmentPagerAdapter 的麻烦和困境:构造函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆